forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAll O`one Data Structure.cpp
45 lines (39 loc) · 1.06 KB
/
All O`one Data Structure.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class AllOne {
public:
map<int,unordered_set<string>> minmax;
unordered_map<string,int> count;
AllOne() {
}
void inc(string key) {
int was = count[key]++;
if(was>0) {
minmax[was].erase(key);
if(minmax[was].size()==0) minmax.erase(was);
}
minmax[was+1].insert(key);
}
void dec(string key) {
int was = count[key]--;
minmax[was].erase(key);
if(minmax[was].size()==0) minmax.erase(was);
if(was-1==0) {
count.erase(key);
}else {
minmax[was-1].insert(key);
}
}
string getMaxKey() {
return minmax.size() == 0 ? "" : *minmax.rbegin()->second.begin();
}
string getMinKey() {
return minmax.size() == 0 ? "" : *minmax.begin()->second.begin();
}
};
/**
* Your AllOne object will be instantiated and called as such:
* AllOne* obj = new AllOne();
* obj->inc(key);
* obj->dec(key);
* string param_3 = obj->getMaxKey();
* string param_4 = obj->getMinKey();
*/