-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllOoneDataStructure.cpp
90 lines (75 loc) · 2.17 KB
/
AllOoneDataStructure.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include<iostream>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
class AllOne {
private:
unordered_map<string, int> data;
unordered_map<int, unordered_set<string> > m;
int min_val = INT_MAX;
int max_val = INT_MIN;
public:
/** Initialize your data structure here. */
AllOne() {
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
void inc(string key) {
data[key] += 1;
if(data[key] > max_val) max_val = data[key];
if(data[key] < min_val) min_val = data[key];
else {
if(min_val == data[key] - 1 && m[min_val].size() == 1) min_val++;
}
m[data[key]].insert(key);
if(data[key] - 1 > 0) m[data[key] - 1].erase(key);
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
data[key] -= 1;
m[data[key] + 1].erase(key);
if(data[key] == 0) {
data.erase(key);
if(m[1].size() == 0) {
if(max_val == 1) max_val = 0;
while(min_val <= max_val && m[min_val].size() == 0) min_val++;
if(min_val > max_val) min_val = 0;
}
}else {
cout<<"max_val: "<<max_val<<" min_val: "<<min_val<<endl;
if(max_val == data[key] + 1 && m[max_val].size() == 0) max_val--;
if(min_val == data[key] + 1) min_val--;
m[data[key]].insert(key);
}
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
if(max_val == 0 || max_val == INT_MIN) return "";
else return *m[max_val].begin();
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
if(min_val == 0 || min_val == INT_MAX) return "";
else return *m[min_val].begin();
}
};
int main() {
AllOne *obj = new AllOne();
obj->inc("a");
obj->inc("b");
obj->inc("b");
obj->inc("b");
obj->inc("b");
obj->dec("b");
obj->dec("b");
cout<<obj->getMaxKey()<<endl;
cout<<obj->getMinKey()<<endl;
// obj->inc("hello");
// obj->inc("hello");
// obj->inc("world");
// obj->dec("world");
//
// cout<<obj->getMaxKey()<<endl;
// cout<<obj->getMinKey()<<endl;
return 0;
}