-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0146_lru_cache.py
More file actions
58 lines (49 loc) · 1.53 KB
/
0146_lru_cache.py
File metadata and controls
58 lines (49 loc) · 1.53 KB
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
from utils.node_helper import DoubleLinkNode
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.dic = {}
self.head = DoubleLinkNode(0, 0)
self.tail = DoubleLinkNode(0, 0)
self.head.next = self.tail
self.tail.pre = self.head
def _add(self, node):
p = self.tail.pre
self.tail.pre = node
p.next = node
node.next = self.tail
node.pre = p
def _remove(self, node):
p = node.pre
n = node.next
p.next = n
n.pre = p
def get(self, key: int) -> int:
if key in self.dic:
node = self.dic[key]
self._remove(node)
self._add(node)
return node.val
else:
return -1
def put(self, key: int, value: int) -> None:
if key in self.dic:
self._remove(self.dic[key])
node = DoubleLinkNode(key, value)
self.dic[key] = node
self._add(node)
if len(self.dic) > self.capacity:
node = self.head.next
self._remove(node)
del self.dic[node.key]
if __name__ == '__main__':
cache = LRUCache(2)
cache.put(1, 1)
cache.put(2, 2)
print(cache.get(1)) # return 1
cache.put(3, 3) # evicts key 2
print(cache.get(2)) # returns -1 (not found)
cache.put(4, 4) # evicts key 1
print(cache.get(1)) # returns -1 (not found)
print(cache.get(3)) # returns 3
print(cache.get(4)) # returns 4