-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathLFUCache460.java
285 lines (252 loc) · 8.81 KB
/
LFUCache460.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
/**
* Design and implement a data structure for Least Frequently Used (LFU) cache.
* It should support the following operations: get and put.
*
* get(key) - Get the value (will always be positive) of the key if the key
* exists in the cache, otherwise return -1.
* put(key, value) - Set or insert the value if the key is not already present.
* When the cache reaches its capacity, it should invalidate the least
* frequently used item before inserting a new item. For the purpose of this
* problem, when there is a tie (i.e., two or more keys that have the same
* frequency), the least recently used key would be evicted.
*
* Follow up:
* Could you do both operations in O(1) time complexity?
*
* Example:
* LFUCache cache = new LFUCache( 2 ); // capacity
*
* cache.put(1, 1);
* cache.put(2, 2);
* cache.get(1); // returns 1
* cache.put(3, 3); // evicts key 2
* cache.get(2); // returns -1 (not found)
* cache.get(3); // returns 3.
* cache.put(4, 4); // evicts key 1.
* cache.get(1); // returns -1 (not found)
* cache.get(3); // returns 3
* cache.get(4); // returns 4
*/
public class LFUCache460 {
class LFUCache {
// freq -> Linked List
private TreeMap<Integer, Node> treeMap = new TreeMap<>();
// key -> Node
private Map<Integer, Node> map = new HashMap<>();
private int capacity;
public LFUCache(int capacity) {
this.capacity = capacity;
}
public int get(int key) {
if (!this.map.containsKey(key)) return -1;
Node node = map.get(key);
deleteFromTreeMap(node);
node.freq++;
addOnTreeMap(node);
return node.val;
}
private void deleteFromList(Node node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void addToListEnd(Node head, Node node) {
node.prev = head.prev;
node.next = head;
head.prev.next = node;
head.prev = node;
}
private void addOnTreeMap(Node node) {
Node head = treeMap.get(node.freq);
if (head == null) {
head = newHead();
}
addToListEnd(head, node);
this.treeMap.put(node.freq, head);
}
private void deleteFromTreeMap(Node node) {
deleteFromList(node);
Node head = treeMap.get(node.freq);
if (head.next.freq == -1) {
this.treeMap.remove(node.freq);
}
}
public void put(int key, int value) {
if (this.capacity == 0) return;
Node node = this.map.get(key);
if (node == null) {
node = new Node(key, value, 1);
if (this.map.size() == this.capacity) {
Map.Entry<Integer, Node> first = this.treeMap.firstEntry();
Integer firstKey = first.getKey();
Node firstHead = first.getValue();
Node toBeRemoved = firstHead.next;
deleteFromTreeMap(toBeRemoved);
this.map.remove(toBeRemoved.key);
}
this.map.put(key, node);
} else {
node.val = value;
deleteFromTreeMap(node);
node.freq++;
}
addOnTreeMap(node);
}
private Node newHead() {
Node head = new Node(0, 0, -1);
head.prev = head;
head.next = head;
return head;
}
class Node {
Node next;
Node prev;
int key;
int val;
int freq;
Node(int key, int val, int freq) {
this.key = key;
this.val = val;
this.freq = freq;
}
}
}
class LFUCache2 {
private FreqNode head;
private Map<Integer, FreqNode> freqMap = new HashMap<>();
private Map<Integer, ItemNode> itemMap = new HashMap<>();
private int capacity;
public LFUCache(int capacity) {
this.capacity = capacity;
this.head = new FreqNode();
this.head.next = this.head;
this.head.prev = this.head;
}
public int get(int key) {
if (!this.itemMap.containsKey(key)) return -1;
ItemNode item = this.itemMap.get(key);
updateNodeFreq(item);
return item.val;
}
private void updateNodeFreq(ItemNode item) {
dislinkNode(item);
Integer freq = item.freq;
item.freq++;
FreqNode currFreqNode = freqMap.get(freq);
FreqNode newFreqNode = freqMap.get(freq+1);
if (newFreqNode == null) {
newFreqNode = new FreqNode(freq+1);
freqMap.put(freq+1, newFreqNode);
addAfter(currFreqNode, newFreqNode);
}
addBefore(newFreqNode.itemHead, item);
if (itemNodeIsEmpty(currFreqNode.itemHead)) {
removeNode(currFreqNode);
}
}
private boolean itemNodeIsEmpty(ItemNode node) {
return node.next == node;
}
private void removeNode(FreqNode node) {
dislinkNode(node);
this.freqMap.remove(node.freq);
}
private void removeNode(ItemNode node) {
dislinkNode(node);
this.itemMap.remove(node.key);
}
private void dislinkNode(FreqNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void dislinkNode(ItemNode node) {
node.prev.next = node.next;
node.next.prev = node.prev;
}
private void addAfter(FreqNode currNode, FreqNode newNode) {
newNode.next = currNode.next;
newNode.prev = currNode;
currNode.next.prev = newNode;
currNode.next = newNode;
}
private void addAfter(ItemNode currNode, ItemNode newNode) {
newNode.next = currNode.next;
newNode.prev = currNode;
currNode.next.prev = newNode;
currNode.next = newNode;
}
private void addBefore(ItemNode currNode, ItemNode newNode) {
newNode.next = currNode;
newNode.prev = currNode.prev;
currNode.prev.next = newNode;
currNode.prev = newNode;
}
public void put(int key, int value) {
if (this.capacity == 0) return;
if (this.itemMap.containsKey(key)) {
ItemNode item = this.itemMap.get(key);
item.val = value;
updateNodeFreq(item);
return;
}
if (this.itemMap.size() == this.capacity) {
FreqNode first = this.head.next;
ItemNode toBeRemoved = first.itemHead.next;
removeNode(toBeRemoved);
if (itemNodeIsEmpty(first.itemHead)) {
removeNode(first);
}
}
FreqNode newFreqNode = freqMap.get(1);
if (newFreqNode == null) {
newFreqNode = new FreqNode(1);
freqMap.put(1, newFreqNode);
addAfter(this.head, newFreqNode);
}
ItemNode newItem = new ItemNode(key, value, 1);
addBefore(newFreqNode.itemHead, newItem);
this.itemMap.put(key, newItem);
}
private ItemNode newItemHead() {
ItemNode head = new ItemNode();
head.prev = head;
head.next = head;
return head;
}
class ItemNode {
ItemNode next;
ItemNode prev;
int key;
int val;
int freq;
ItemNode(int key, int val, int freq) {
this.key = key;
this.val = val;
this.freq = freq;
}
ItemNode() {
this.key = -1;
this.val = -1;
this.freq = -1;
}
}
class FreqNode {
FreqNode next;
FreqNode prev;
int freq;
ItemNode itemHead;
FreqNode(int freq) {
this.freq = freq;
this.itemHead = newItemHead();
}
FreqNode() {
this.freq = -1;
}
}
}
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache obj = new LFUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
}