-
Notifications
You must be signed in to change notification settings - Fork 3
/
2349.cpp
30 lines (26 loc) · 857 Bytes
/
2349.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class NumberContainers {
public:
unordered_map<int, int> index2num;
unordered_map<int, set<int>> num2indices;
NumberContainers() {
}
void change(int index, int number) {
if (index2num.find(index) != index2num.end()) {
int prevNum = index2num[index];
num2indices[prevNum].erase(index);
}
index2num[index] = number;
num2indices[number].insert(index);
}
int find(int number) {
if (num2indices.find(number) == num2indices.end()) return -1;
if (num2indices[number].size() == 0) return -1;
return *(num2indices[number].begin());
}
};
/**
* Your NumberContainers object will be instantiated and called as such:
* NumberContainers* obj = new NumberContainers();
* obj->change(index,number);
* int param_2 = obj->find(number);
*/