-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path460.LFU-Cache.py
144 lines (114 loc) · 3.88 KB
/
460.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
# https://leetcode.com/problems/lfu-cache/
#
# algorithms
# Hard (29.8%)
# Total Accepted: 45,876
# Total Submissions: 153,943
class Heap(object):
def __init__(self):
self.h = []
def __len__(self):
return len(self.h)
def _up(self, idx):
while idx > 0:
parent_idx = (idx - 1) / 2
if parent_idx < 0:
break
if self.h[idx].freq < self.h[parent_idx].freq or (
self.h[idx].freq == self.h[parent_idx].freq and self.h[idx].ts < self.h[parent_idx].ts):
self.h[idx], self.h[parent_idx] = self.h[parent_idx], self.h[idx]
self.h[idx].idx, self.h[parent_idx].idx = idx, parent_idx
idx = parent_idx
else:
break
def _down(self, idx):
bound = len(self.h)
while idx < bound:
left_child_idx = 2 * idx + 1
if left_child_idx >= bound:
break
min_child_idx = left_child_idx
right_child_idx = left_child_idx + 1
if right_child_idx < bound and (self.h[right_child_idx].freq < self.h[left_child_idx].freq or (
self.h[right_child_idx].freq == self.h[left_child_idx].freq and
self.h[right_child_idx].ts < self.h[left_child_idx].ts
)):
min_child_idx = right_child_idx
if self.h[min_child_idx].freq < self.h[idx].freq or (
self.h[min_child_idx].freq == self.h[idx].freq and self.h[min_child_idx].ts < self.h[idx].ts):
self.h[idx], self.h[min_child_idx] = self.h[min_child_idx], self.h[idx]
self.h[idx].idx, self.h[min_child_idx].idx = idx, min_child_idx
idx = min_child_idx
else:
break
def heappush(self, item):
idx = len(self.h)
item.idx = idx
self.h.append(item)
self._up(idx)
def heappop(self):
length = len(self.h)
if length == 0:
return
self.h[0], self.h[length - 1] = self.h[length - 1], self.h[0]
res = self.h.pop()
self._down(0)
return res
def heapupdate(self, idx):
self._down(idx)
class Node(object):
def __init__(self, k, v, ts):
self.k = k
self.v = v
self.freq = 1 # 频率
self.ts = ts # 创建先后
self.idx = -1 # 位于堆中的下标
class LFUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.h = Heap()
self.hash_map = {}
self.ts_generator = 0
self.cap = capacity
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key in self.hash_map:
self.hash_map[key].freq += 1
self.hash_map[key].ts = self.ts_generator
self.ts_generator += 1
self.h.heapupdate(self.hash_map[key].idx)
return self.hash_map[key].v
else:
return -1
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
if self.cap == 0:
return
if key in self.hash_map:
self.hash_map[key].v = value
self.hash_map[key].ts = self.ts_generator
self.hash_map[key].freq += 1
self.ts_generator += 1
self.h.heapupdate(self.hash_map[key].idx)
return
if len(self.h) == self.cap:
remove_item = self.h.heappop()
if remove_item:
del self.hash_map[remove_item.k]
new_item = Node(key, value, self.ts_generator)
self.hash_map[key] = new_item
self.ts_generator += 1
self.h.heappush(new_item)
# Your LFUCache object will be instantiated and called as such:
# obj = LFUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)