Skip to content

Commit 8c090e5

Browse files
committed
Add C++ solution of problem #2349
1 parent 0fecbc6 commit 8c090e5

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

2349/solution.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class NumberContainers {
2+
public:
3+
NumberContainers() {
4+
5+
}
6+
7+
void change(int index, int number) {
8+
if(!m.count(index)) {
9+
m[index] = number;
10+
mm[number].insert(index);
11+
return;
12+
}
13+
14+
int num = m[index];
15+
mm[num].erase(index);
16+
m[index] = number;
17+
mm[number].insert(index);
18+
}
19+
20+
int find(int number) {
21+
if(!mm.count(number) || mm[number].size() < 1)
22+
return -1;
23+
24+
return *(mm[number].begin());
25+
}
26+
27+
private:
28+
unordered_map<int, int> m; // {idx, num}
29+
unordered_map<int, set<int>> mm; // {num, idxs (sorted)}
30+
};
31+
32+
/**
33+
* Your NumberContainers object will be instantiated and called as such:
34+
* NumberContainers* obj = new NumberContainers();
35+
* obj->change(index,number);
36+
* int param_2 = obj->find(number);
37+
*/

0 commit comments

Comments
 (0)