Skip to content

Commit 89d1428

Browse files
authored
Create 432. All O`one Data Structure (#599)
2 parents afa0694 + e293a88 commit 89d1428

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

432. All O`one Data Structure

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class AllOne {
2+
public:
3+
unordered_map<string,int> count;
4+
set<pair<int,string>> se;
5+
AllOne() {
6+
count.clear();
7+
}
8+
9+
void inc(string key) {
10+
int n = count[key];
11+
count[key]++;
12+
se.erase({n,key});
13+
se.insert({n+1,key});
14+
}
15+
void dec(string key) {
16+
int n = count[key];
17+
count[key]--;
18+
se.erase({n,key});
19+
if(count[key] > 0) se.insert({n-1,key});
20+
}
21+
22+
string getMaxKey() {
23+
if(!se.empty()) return se.rbegin()->second;
24+
return "";
25+
}
26+
27+
string getMinKey() {
28+
if(!se.empty()) return se.begin()->second;
29+
return "";
30+
}
31+
};
32+
33+
/**
34+
* Your AllOne object will be instantiated and called as such:
35+
* AllOne* obj = new AllOne();
36+
* obj->inc(key);
37+
* obj->dec(key);
38+
* string param_3 = obj->getMaxKey();
39+
* string param_4 = obj->getMinKey();
40+
*/

0 commit comments

Comments
 (0)