forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
all-oone-data-structure.cpp
79 lines (66 loc) · 2.19 KB
/
all-oone-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
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
// Time: O(1), per operation
// Space: O(k)
class AllOne {
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 (!bucketOfKey_.count(key)) {
bucketOfKey_[key] = buckets_.insert(buckets_.begin(), {0, {key}});
}
auto next = bucketOfKey_[key], bucket = next++;
if (next == buckets_.end() || next->value > bucket->value + 1) {
next = buckets_.insert(next, {bucket->value + 1, {}});
}
next->keys.insert(key);
bucketOfKey_[key] = next;
bucket->keys.erase(key);
if (bucket->keys.empty()) {
buckets_.erase(bucket);
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
if (!bucketOfKey_.count(key)) {
return;
}
auto prev = bucketOfKey_[key], bucket = prev--;
bucketOfKey_.erase(key);
if (bucket->value > 1) {
if (bucket == buckets_.begin() || prev->value < bucket->value - 1) {
prev = buckets_.insert(bucket, {bucket->value - 1, {}});
}
prev->keys.insert(key);
bucketOfKey_[key] = prev;
}
bucket->keys.erase(key);
if (bucket->keys.empty()) {
buckets_.erase(bucket);
}
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
return buckets_.empty() ? "" : *(buckets_.rbegin()->keys.begin());
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
return buckets_.empty() ? "" : *(buckets_.begin()->keys.begin());
}
private:
struct Bucket {
int value;
unordered_set<string> keys;
};
list<Bucket> buckets_;
unordered_map<string, list<Bucket>::iterator> bucketOfKey_;
};
/**
* 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();
*/