-
Notifications
You must be signed in to change notification settings - Fork 28
/
All_O_one_Data_Structure.cpp
74 lines (69 loc) · 2.59 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
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
class AllOne {
public:
/** Initialize your data structure here. */
AllOne() {
freqKeyList = FreqKeyList();
keyFreqKeyListMap = unordered_map<string, FreqKeyList::iterator> ();
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
void inc(string key) {
if (keyFreqKeyListMap.find(key) == keyFreqKeyListMap.end()) {
if (freqKeyList.empty() || freqKeyList.begin()->first != 1) {
freqKeyList.insert(freqKeyList.begin(), make_pair(1, KeySet()));
}
freqKeyList.begin()->second.insert(key);
keyFreqKeyListMap[key] = freqKeyList.begin();
return;
}
FreqKeyList::iterator it = keyFreqKeyListMap[key];
FreqKeyList::iterator next = it;
++next;
if(next != freqKeyList.end() and next->first == it->first + 1) {
next->second.insert(key);
} else {
next = freqKeyList.insert(next, make_pair(keyFreqKeyListMap[key]->first + 1, KeySet({ key })));
}
keyFreqKeyListMap[key] = next;
it->second.erase(key);
if (it->second.empty()) {
freqKeyList.erase(it);
}
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
void dec(string key) {
if (keyFreqKeyListMap.find(key) == keyFreqKeyListMap.end()) {
return;
}
FreqKeyList::iterator it = keyFreqKeyListMap[key];
FreqKeyList::iterator prev = it;
if (it != freqKeyList.begin()) {
prev--;
}
if (it->first != 1) {
if (it == freqKeyList.begin() or prev->first != it->first - 1) {
prev = freqKeyList.insert(it, make_pair(keyFreqKeyListMap[key]->first - 1, KeySet()));
}
prev->second.insert(key);
keyFreqKeyListMap[key] = prev;
} else {
keyFreqKeyListMap.erase(key);
}
it->second.erase(key);
if (it->second.empty()) {
freqKeyList.erase(it);
}
}
/** Returns one of the keys with maximal value. */
string getMaxKey() {
return freqKeyList.empty() ? "" : *freqKeyList.rbegin()->second.begin();
}
/** Returns one of the keys with Minimal value. */
string getMinKey() {
return freqKeyList.empty() ? "" : *freqKeyList.begin()->second.begin();
}
private:
typedef unordered_set<string> KeySet;
typedef list<pair<int, KeySet>> FreqKeyList;
FreqKeyList freqKeyList;
unordered_map<string, FreqKeyList::iterator> keyFreqKeyListMap;
};