forked from subsr97/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlfu-cache.py
105 lines (81 loc) · 3.05 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
"""
#67
Google
Implement an LFU (Least Frequently Used) cache.
It should be able to be initialized with a cache size n, and contain the following methods:
set(key, value): sets key to value.
If there are already n items in the cache and we are adding a new item, then it should also remove the least frequently used item.
If there is a tie, then the least recently used key should be removed.
get(key): gets the value at key. If no such key exists, return null.
Each operation should run in O(1) time.
"""
class LFUCache:
def __init__(self, capacity: int):
self.cache = dict()
self.countDict = dict()
self.grouping = dict()
self.capacity = capacity
def get(self, key: int) -> int:
if key in self.cache:
currentCount = self.countDict[key]
self.countDict[key] = currentCount+1
self.grouping[currentCount].remove(key)
if len(self.grouping[currentCount]) == 0:
del self.grouping[currentCount]
if currentCount+1 in self.grouping:
self.grouping[currentCount+1].append(key)
else:
self.grouping[currentCount+1] = [key]
return self.cache[key]
else:
return -1
def put(self, key: int, value: int) -> None:
if self.capacity <= 0:
return
if key in self.cache:
self.cache[key] = value
currentCount = self.countDict[key]
self.countDict[key] = currentCount+1
self.grouping[currentCount].remove(key)
if len(self.grouping[currentCount]) == 0:
del self.grouping[currentCount]
if currentCount+1 in self.grouping:
self.grouping[currentCount+1].append(key)
else:
self.grouping[currentCount+1] = [key]
else:
if len(self.countDict) >= self.capacity:
lfuCount = min(self.grouping.keys())
lfuKey = self.grouping[lfuCount].pop(0)
print(self.grouping[lfuCount])
if len(self.grouping[lfuCount]) == 0:
del self.grouping[lfuCount]
del self.cache[lfuKey]
del self.countDict[lfuKey]
self.cache[key] = value
if 1 in self.grouping:
self.grouping[1].append(key)
else:
self.grouping[1] = [key]
self.countDict[key] = 1
def __str__(self):
return "Capacity: {}\nCache: {}\nCount Dict: {}\nGrouping Dict: {}\n".format(self.capacity, self.cache, self.countDict, self.grouping)
def main():
lfuCache = LFUCache(2)
print(lfuCache)
lfuCache.put(1,1)
print(lfuCache)
lfuCache.put(2,2)
print(lfuCache)
print(lfuCache.get(1))
lfuCache.put(3,3)
print(lfuCache)
print(lfuCache.get(2))
print(lfuCache.get(3))
lfuCache.put(4,4)
print(lfuCache)
print(lfuCache.get(1))
print(lfuCache.get(3))
print(lfuCache.get(4))
if __name__ == "__main__":
main()