-
Notifications
You must be signed in to change notification settings - Fork 43
/
lfu_cache.py
420 lines (364 loc) · 13 KB
/
lfu_cache.py
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
460. LFU Cache
Hard
Design and implement a data structure for a Least Frequently Used (LFU) cache.
Implement the LFUCache class:
LFUCache(int capacity) Initializes the object with the capacity of the data structure.
int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
void put(int key, int value) Update the value of the key if present, or inserts the key if not already present. When the cache reaches its capacity, it should invalidate and remove the least frequently used key before inserting a new item. For this problem, when there is a tie (i.e., two or more keys with the same frequency), the least recently used key would be invalidated.
To determine the least frequently used key, a use counter is maintained for each key in the cache. The key with the smallest use counter is the least frequently used key.
When a key is first inserted into the cache, its use counter is set to 1 (due to the put operation). The use counter for a key in the cache is incremented either a get or put operation is called on it.
The functions get and put must each run in O(1) average time complexity.
Example 1:
Input
["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, 3, null, -1, 3, 4]
Explanation
// cnt(x) = the use counter for key x
// cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
LFUCache lfu = new LFUCache(2);
lfu.put(1, 1); // cache=[1,_], cnt(1)=1
lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lfu.get(1); // return 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
// cache=[3,1], cnt(3)=1, cnt(1)=2
lfu.get(2); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
// cache=[4,3], cnt(4)=1, cnt(3)=2
lfu.get(1); // return -1 (not found)
lfu.get(3); // return 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lfu.get(4); // return 4
// cache=[3,4], cnt(4)=2, cnt(3)=3
Constraints:
0 <= capacity <= 104
0 <= key <= 105
0 <= value <= 109
At most 2 * 105 calls will be made to get and put.
"""
# V0
from collections import OrderedDict
class Node:
def __init__(self, key, val, count):
self.key=key
self.val=val
self.count=count
class LFUCache:
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity=capacity
self.key_node={}
self.count_node={}
self.minV=None
def get(self, key):
"""
:type key: int
:rtype: int
"""
if not key in self.key_node: return -1
node = self.key_node[key]
del self.count_node[node.count][key]
if not self.count_node[node.count]:
del self.count_node[node.count]
node.count+=1
if not node.count in self.count_node:
self.count_node[node.count]=OrderedDict()
self.count_node[node.count][key]=node
if not self.minV in self.count_node:
self.minV+=1
return node.val
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: void
"""
# if element exists, -> update value and count + 1
if self.capacity==0: return None
if key in self.key_node:
self.key_node[key].val=value
self.get(key)
else:
if len(self.key_node) == self.capacity:
item=self.count_node[self.minV].popitem(last=False)
del self.key_node[item[0]]
node=Node(key,value,1)
self.key_node[key]=node
if not 1 in self.count_node:
self.count_node[1]=OrderedDict()
self.count_node[1][key]=node
self.minV=1
# V1
# https://blog.csdn.net/Neekity/article/details/84765476
from collections import OrderedDict
class Node:
def __init__(self, key, val, count):
self.key=key
self.val=val
self.count=count
class LFUCache:
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity=capacity
self.key_node={}
self.count_node={}
self.minV=None
def get(self, key):
"""
:type key: int
:rtype: int
"""
if not key in self.key_node: return -1
node = self.key_node[key]
del self.count_node[node.count][key]
if not self.count_node[node.count]:
del self.count_node[node.count]
node.count+=1
if not node.count in self.count_node:
self.count_node[node.count]=OrderedDict()
self.count_node[node.count][key]=node
if not self.minV in self.count_node:
self.minV+=1
return node.val
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: void
"""
# if element exists, -> update value and count + 1
if self.capacity==0: return None
if key in self.key_node:
self.key_node[key].val=value
self.get(key)
else:
if len(self.key_node) == self.capacity:
item=self.count_node[self.minV].popitem(last=False)
del self.key_node[item[0]]
node=Node(key,value,1)
self.key_node[key]=node
if not 1 in self.count_node:
self.count_node[1]=OrderedDict()
self.count_node[1][key]=node
self.minV=1
# V2
# https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/lfu-cache.py
# Time: O(1), per operation
# Space: O(k), k is the capacity of cache
import collections
class ListNode(object):
def __init__(self, key, value, freq):
self.key = key
self.val = value
self.freq = freq
self.next = None
self.prev = None
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
def append(self, node):
node.next, node.prev = None, None # avoid dirty node
if self.head is None:
self.head = node
else:
self.tail.next = node
node.prev = self.tail
self.tail = node
def delete(self, node):
if node.prev:
node.prev.next = node.next
else:
self.head = node.next
if node.next:
node.next.prev = node.prev
else:
self.tail = node.prev
node.next, node.prev = None, None # make node clean
class LFUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.__capa = capacity
self.__size = 0
self.__min_freq = 0
self.__freq_to_nodes = collections.defaultdict(LinkedList)
self.__key_to_node = {}
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key not in self.__key_to_node:
return -1
old_node = self.__key_to_node[key]
self.__key_to_node[key] = ListNode(key, old_node.val, old_node.freq)
self.__freq_to_nodes[old_node.freq].delete(old_node)
if not self.__freq_to_nodes[self.__key_to_node[key].freq].head:
del self.__freq_to_nodes[self.__key_to_node[key].freq]
if self.__min_freq == self.__key_to_node[key].freq:
self.__min_freq += 1
self.__key_to_node[key].freq += 1
self.__freq_to_nodes[self.__key_to_node[key].freq].append(self.__key_to_node[key])
return self.__key_to_node[key].val
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: void
"""
if self.__capa <= 0:
return
if self.get(key) != -1:
self.__key_to_node[key].val = value
return
if self.__size == self.__capa:
del self.__key_to_node[self.__freq_to_nodes[self.__min_freq].head.key]
self.__freq_to_nodes[self.__min_freq].delete(self.__freq_to_nodes[self.__min_freq].head)
if not self.__freq_to_nodes[self.__min_freq].head:
del self.__freq_to_nodes[self.__min_freq]
self.__size -= 1
self.__min_freq = 1
self.__key_to_node[key] = ListNode(key, value, self.__min_freq)
self.__freq_to_nodes[self.__key_to_node[key].freq].append(self.__key_to_node[key])
self.__size += 1
# V1
# TODO : fix this
# IDEA : DOUBLY LINKED LIST + HASH TABLE
# http://bookshadow.com/weblog/2016/11/22/leetcode-lfu-cache/
# https://www.cnblogs.com/grandyang/p/6258459.html
# class KeyNode(object):
# def __init__(self, key, value, freq = 1):
# self.key = key
# self.value = value
# self.freq = freq
# self.prev = self.next = None
# class FreqNode(object):
# def __init__(self, freq, prev, next):
# self.freq = freq
# self.prev = prev
# self.next = next
# self.first = self.last = None
# class LFUCache(object):
# def __init__(self, capacity):
# """
# :type capacity: int
# """
# self.capacity = capacity
# self.keyDict = dict()
# self.freqDict = dict()
# self.head = None
# def get(self, key):
# """
# :type key: int
# :rtype: int
# """
# if key in self.keyDict:
# keyNode = self.keyDict[key]
# value = keyNode.value
# self.increase(key, value)
# return value
# return -1
# def set(self, key, value):
# """
# :type key: int
# :type value: int
# :rtype: void
# """
# if self.capacity == 0:
# return
# if key in self.keyDict:
# self.increase(key, value)
# return
# if len(self.keyDict) == self.capacity:
# self.removeKeyNode(self.head.last)
# self.insertKeyNode(key, value)
# def increase(self, key, value):
# """
# Increments the freq of an existing KeyNode<key, value> by 1.
# :type key: str
# :rtype: void
# """
# keyNode = self.keyDict[key]
# keyNode.value = value
# freqNode = self.freqDict[keyNode.freq]
# nextFreqNode = freqNode.next
# keyNode.freq += 1
# if nextFreqNode is None or nextFreqNode.freq > keyNode.freq:
# nextFreqNode = self.insertFreqNodeAfter(keyNode.freq, freqNode)
# self.unlinkKey(keyNode, freqNode)
# self.linkKey(keyNode, nextFreqNode)
# def insertKeyNode(self, key, value):
# """
# Inserts a new KeyNode<key, value> with freq 1.
# :type key: str
# :rtype: void
# """
# keyNode = self.keyDict[key] = KeyNode(key, value)
# freqNode = self.freqDict.get(1)
# if freqNode is None:
# freqNode = self.freqDict[1] = FreqNode(1, None, self.head)
# if self.head:
# self.head.prev = freqNode
# self.head = freqNode
# self.linkKey(keyNode, freqNode)
# def delFreqNode(self, freqNode):
# """
# Delete freqNode.
# :rtype: void
# """
# prev, next = freqNode.prev, freqNode.next
# if prev: prev.next = next
# if next: next.prev = prev
# if self.head == freqNode: self.head = next
# del self.freqDict[freqNode.freq]
# def insertFreqNodeAfter(self, freq, node):
# """
# Insert a new FreqNode(freq) after node.
# :rtype: FreqNode
# """
# newNode = FreqNode(freq, node, node.next)
# self.freqDict[freq] = newNode
# if node.next: node.next.prev = newNode
# node.next = newNode
# return newNode
# def removeKeyNode(self, keyNode):
# """
# Remove keyNode
# :rtype: void
# """
# self.unlinkKey(keyNode, self.freqDict[keyNode.freq])
# del self.keyDict[keyNode.key]
# def unlinkKey(self, keyNode, freqNode):
# """
# Unlink keyNode from freqNode
# :rtype: void
# """
# next, prev = keyNode.next, keyNode.prev
# if prev: prev.next = next
# if next: next.prev = prev
# if freqNode.first == keyNode: freqNode.first = next
# if freqNode.last == keyNode: freqNode.last = prev
# if freqNode.first is None: self.delFreqNode(freqNode)
# def linkKey(self, keyNode, freqNode):
# """
# Link keyNode to freqNode
# :rtype: void
# """
# firstKeyNode = freqNode.first
# keyNode.prev = None
# keyNode.next = firstKeyNode
# if firstKeyNode: firstKeyNode.prev = keyNode
# freqNode.first = keyNode
# if freqNode.last is None: freqNode.last = keyNode
# V2