-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllOoneDataStructure2.cpp
115 lines (93 loc) · 2.56 KB
/
AllOoneDataStructure2.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include<list>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
using namespace std;
class AllOne {
private:
struct Bucket {
int val;
unordered_set<string> keys;
};
list<Bucket> buckets;
unordered_map<string, list<Bucket>::iterator> m;
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) {
if(m.find(key) != m.end()) {
list<Bucket>::iterator it = m[key];
list<Bucket>::iterator next = ++m[key];
it->keys.erase(key);
if(next->val != it->val + 1) {
list<Bucket>::iterator newBucket = buckets.insert(next, {it->val + 1, {key}});
m[key] = newBucket;
}else {
next->keys.insert(key);
m[key] = next;
}
if(it->keys.empty()) buckets.erase(it);
}else {
if(buckets.front().val != 1) {
m[key] = buckets.insert(buckets.begin(), {1, {key}});
}else {
buckets.begin()->keys.insert(key);
m[key] = buckets.begin();
}
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
if(m.find(key) == m.end()) return;
list<Bucket>::iterator it = m[key];
list<Bucket>::iterator last = --m[key];
it->keys.erase(key);
if(it->val == 1) {
if(it->keys.empty()) buckets.erase(it);
m.erase(key);
return;
}
if(it->val - 1 != last->val) {
list<Bucket>::iterator newBucket = buckets.insert(it, {it->val - 1, {key}});
m[key] = newBucket;
}
else {
last->keys.insert(key);
m[key] = last;
}
if(it->keys.empty()) buckets.erase(it);
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
if(buckets.empty()) return "";
else return *(buckets.rbegin()->keys.begin());
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
if(buckets.empty()) return "";
else return *(buckets.begin()->keys.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;
}