Skip to content

Commit 11820a5

Browse files
committed
feat: Solve leetcode/leetcoding_challenge/2025/feb2025/week2/design_a_number_container_system.cpp
1 parent 83ab1ca commit 11820a5

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include <algorithm>
2+
#include <vector>
3+
#include <queue>
4+
#include <set>
5+
#include <limits>
6+
#include <map>
7+
#include <unordered_set>
8+
#include <unordered_map>
9+
#include <iterator>
10+
#include <sstream>
11+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
12+
using namespace std; // since cin and cout are both in namespace std, this saves some text
13+
14+
class NumberContainers {
15+
unordered_map<int, int> arr;
16+
unordered_map<int, set<int>> container;
17+
18+
public:
19+
NumberContainers() {
20+
arr = unordered_map<int, int>();
21+
container = unordered_map<int, set<int>>();
22+
}
23+
24+
void change(int index, int number) {
25+
// 1. remove from container if it is already asssigned in arr
26+
if (arr.count(index) > 0) {
27+
auto currNum = arr[index];
28+
container[currNum].erase(index);
29+
if (container[currNum].size() == 0) {
30+
container.erase(currNum);
31+
}
32+
}
33+
34+
arr[index] = number;
35+
container[number].insert(index);
36+
}
37+
38+
int find(int number) {
39+
if (container.count(number) == 0) {
40+
return -1;
41+
}
42+
return *begin(container[number]);
43+
}
44+
};
45+
46+
/**
47+
* Your NumberContainers object will be instantiated and called as such:
48+
* NumberContainers* obj = new NumberContainers();
49+
* obj->change(index,number);
50+
* int param_2 = obj->find(number);
51+
*/

0 commit comments

Comments
 (0)