-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-432-All-O-one-Data-Structure.java
307 lines (257 loc) · 9.59 KB
/
LeetCode-432-All-O-one-Data-Structure.java
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
tail <-> prev <-> curr <-> next <-> head
Be very careful:
1. Update nodes (add or delete) by removing node or adding node, we have to update both the countToNode map, and the list.
2. When inc, the original node could be empty. Check if it's empty, if it is remove the Node from countToNode map and the list.
*/
// class AllOne {
// private class Node {
// int count;
// Set<String> keys;
// Node prev, next;
// public Node(int count) {
// this.count = count;
// keys = new HashSet<>();
// }
// }
// HashMap<String, Integer> keyToFreq; // <key, freq> pair
// HashMap<Integer, Node> freqToNode; // <freq, Node> pair
// Node head, tail;
// /** Initialize your data structure here. */
// public AllOne() {
// keyToFreq = new HashMap<>();
// freqToNode = new HashMap<>();
// head = new Node(Integer.MAX_VALUE);
// tail = new Node(Integer.MIN_VALUE);
// head.prev = tail;
// tail.next = head;
// }
// /** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
// public void inc(String key) {
// int freq = 0;
// if (keyToFreq.containsKey(key)) {
// freq = keyToFreq.get(key);
// }
// keyToFreq.put(key, freq + 1);
// Node curr;
// if (freqToNode.containsKey(freq)) {
// curr = freqToNode.get(freq);
// } else {
// curr = tail;
// }
// if (!freqToNode.containsKey(freq + 1)) {
// freqToNode.put(freq + 1, new Node(freq + 1));
// insertAfter(curr, freqToNode.get(freq + 1));
// }
// curr.keys.remove(key);
// if (curr.keys.isEmpty() && curr != tail) {
// deleteNode(curr);
// freqToNode.remove(freq);
// }
// Node next = freqToNode.get(freq + 1);
// next.keys.add(key);
// }
// /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
// public void dec(String key) {
// if (!keyToFreq.containsKey(key)) return;
// int freq = keyToFreq.get(key);
// keyToFreq.put(key, freq - 1);
// if (keyToFreq.get(key) == 0) {
// keyToFreq.remove(key);
// }
// Node curr = freqToNode.get(freq);
// if (freq - 1 > 0 && !freqToNode.containsKey(freq - 1)) {
// freqToNode.put(freq - 1, new Node(freq - 1));
// insertBefore(curr, freqToNode.get(freq - 1));
// }
// if (freq - 1 > 0) {
// Node prev = freqToNode.get(freq - 1);
// prev.keys.add(key);
// }
// curr.keys.remove(key);
// if (curr.keys.isEmpty()) {
// deleteNode(curr);
// freqToNode.remove(freq);
// }
// }
// /** Returns one of the keys with maximal value. */
// public String getMaxKey() {
// // printList(tail);
// // printListFromHead(head);
// // System.out.println(head.prev.count + "-" + head.prev.keys.toString());
// return head.prev.keys.iterator().hasNext() ? head.prev.keys.iterator().next() : "";
// }
// /** Returns one of the keys with Minimal value. */
// public String getMinKey() {
// return tail.next.keys.iterator().hasNext() ? tail.next.keys.iterator().next() : "";
// }
// private void insertAfter(Node curr, Node next) {
// next.next = curr.next;
// curr.next.prev = next;
// curr.next = next;
// next.prev = curr;
// }
// private void insertBefore(Node curr, Node prev) {
// prev.prev = curr.prev;
// curr.prev.next = prev;
// curr.prev = prev;
// prev.next = curr;
// }
// private void deleteNode(Node curr) {
// if (curr.next != null) curr.next.prev = curr.prev;
// if (curr.prev != null) curr.prev.next = curr.next;
// curr.prev = null;
// curr.next = null;
// }
// private void printList(Node tail) {
// StringBuilder sb = new StringBuilder();
// Node p = tail;
// sb.append(p.count + "-" + p.keys.toString());
// p = p.next;
// while (p != null) {
// sb.append(" -> ").append(p.count + "-" + p.keys.toString());
// p = p.next;
// }
// System.out.println(sb.toString());
// }
// private void printListFromHead(Node head) {
// StringBuilder sb = new StringBuilder();
// Node p = head;
// while (p != null) {
// sb.insert(0, p.count + "-" + p.keys.toString());
// sb.insert(0, "->");
// p = p.prev;
// }
// System.out.println(sb.toString());
// }
// }
// 2. A more concise approach (Using insertAfter to do inc and dec)
class AllOne {
private class Node {
int count;
Set<String> keys;
Node prev, next;
public Node(int count) {
this.count = count;
this.keys = new HashSet<>();
}
}
HashMap<String, Integer> keyToCount; // store <key, count> pair
HashMap<Integer, Node> countToNode; // store <count, node> pair
Node head, tail;
/** Initialize your data structure here. */
public AllOne() {
this.keyToCount = new HashMap<>();
this.countToNode = new HashMap<>();
this.head = new Node(Integer.MAX_VALUE);
this.tail = new Node(Integer.MIN_VALUE);
head.prev = tail;
tail.next = head;
}
/** Inserts a new key <Key> with value 1. Or increments an existing key by 1. */
public void inc(String key) {
// 1. Prepare currCount and currNode
int currCount = 0;
if (keyToCount.containsKey(key)) {
currCount = keyToCount.get(key);
}
Node currNode = tail;
if (countToNode.containsKey(currCount)) {
currNode = countToNode.get(currCount);
}
// 2. Prepare newCount and newNode
int newCount = currCount + 1;
if (!countToNode.containsKey(newCount)) {
countToNode.put(newCount, new Node(newCount));
insertAfter(currNode, countToNode.get(newCount));
}
Node newNode = countToNode.get(newCount);
// 3. Update the <key, count> pair and remove the key from original node, remove the original node if necessasry
keyToCount.put(key, newCount);
if (currNode != tail) {
currNode.keys.remove(key);
}
if (currNode != tail && currNode.keys.isEmpty()) {
deleteNode(currNode);
countToNode.remove(currCount);
}
// 4. Add the key to the new Node
newNode.keys.add(key);
}
/** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
public void dec(String key) {
if (!keyToCount.containsKey(key)) return;
// 1. Prepare the currCount and currNode
int currCount = keyToCount.get(key);
Node currNode = countToNode.get(currCount);
// 2. Prepare the newCount and newNode
int newCount = currCount - 1;
if (newCount != 0 && !countToNode.containsKey(newCount)) {
countToNode.put(newCount, new Node(newCount));
insertAfter(currNode.prev, countToNode.get(newCount));
}
// 3. Update the <key, count> pair (remove if necessary), and remove the key from Nodes (remove node from countToNode pair and list if necessary)
keyToCount.put(key, newCount);
if (newCount == 0) {
keyToCount.remove(key);
}
currNode.keys.remove(key);
if (currNode.keys.isEmpty()) {
deleteNode(currNode);
countToNode.remove(currCount);
}
// 4. Add the key to the new node if necessary
if (newCount != 0 && countToNode.containsKey(newCount)) {
countToNode.get(newCount).keys.add(key);
}
}
/** Returns one of the keys with maximal value. */
public String getMaxKey() {
return this.head.prev.keys.iterator().hasNext() ? this.head.prev.keys.iterator().next() : "";
}
/** Returns one of the keys with Minimal value. */
public String getMinKey() {
return this.tail.next.keys.iterator().hasNext() ? this.tail.next.keys.iterator().next() : "";
}
private void insertAfter(Node curr, Node next) {
next.next = curr.next;
curr.next.prev = next;
curr.next = next;
next.prev = curr;
}
private void deleteNode(Node curr) {
curr.prev.next = curr.next;
curr.next.prev = curr.prev;
curr.next = null;
curr.prev = null;
}
// private void printList(Node tail) {
// StringBuilder sb = new StringBuilder();
// Node p = tail;
// sb.append(p.count + "-" + p.keys.toString());
// p = p.next;
// while (p != null) {
// sb.append(" -> ").append(p.count + "-" + p.keys.toString());
// p = p.next;
// }
// System.out.println(sb.toString());
// }
// private void printListFromHead(Node head) {
// StringBuilder sb = new StringBuilder();
// Node p = head;
// while (p != null) {
// sb.insert(0, p.count + "-" + p.keys.toString());
// sb.insert(0, "->");
// p = p.prev;
// }
// System.out.println(sb.toString());
// }
}
/**
* 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();
*/