-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru_cache.py
57 lines (45 loc) · 1.38 KB
/
lru_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
class Node:
def __init__(self, key, value):
self.key = key
self.value = value
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.d = {}
self.least_recent = Node(0,0)
self.most_recent = Node(0, 0)
self.least_recent.next = self.most_recent
self.most_recent.prev = self.least_recent
def remove(self, node):
node.prev.next = node.next
node.next.prev = node.prev
def add(self, node):
node.prev = self.most_recent.prev
node.next = self.most_recent
self.most_recent.prev.next = node
self.most_recent.prev = node
def get(self, key: int) -> int:
if key in self.d:
node = self.d[key]
self.remove(node)
self.add(node)
return node.value
return -1
def put(self, key: int, value: int):
if key in self.d:
self.remove(self.d[key])
elif len(self.d) == self.capacity:
least_recent = self.least_recent.next
self.remove(least_recent)
self.d.pop(least_recent.key)
new_node = Node(key, value)
self.add(new_node)
self.d[key] = new_node
# cache = LRUCache(2)
# cache.put(1,1)
# cache.put(2,2)
# print(cache.get(1))
# cache.put(3,3)
# print(cache.get(2))