-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.py
66 lines (57 loc) · 1.69 KB
/
Solution.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
class Node:
def __init__(self, value: int, key: int = -1):
self.value = value
self.key = key
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.cache = {}
self.capacity = capacity
self.head = Node(-1)
self.tail = Node(-1)
self.head.next = self.tail
self.tail.prev = self.head
def get(self, key: int) -> int:
if key not in self.cache:
return -1
result = self.cache[key]
self.moveToHead(result)
return result.value
def put(self, key: int, value: int) -> None:
if key in self.cache:
self.cache[key].value = value
self.moveToHead(self.cache[key])
else:
if len(self.cache) + 1 > self.capacity:
self.removeNode(self.tail.prev)
newNode = Node(value, key)
self.addToHead(newNode)
def moveToHead(self, node: Node):
self.removeNode(node)
self.addToHead(node)
def addToHead(self, node: Node):
node.next = self.head.next
self.head.next.prev = node
self.head.next = node
node.prev = self.head
self.cache[node.key] = node
def removeNode(self, node: Node):
node.prev.next = node.next
node.next.prev = node.prev
del self.cache[node.key]
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
if __name__ == "__main__":
obj = LRUCache(2)
obj.put(1, 1)
obj.put(2, 2)
obj.get(1)
obj.put(3, 3)
obj.get(2)
obj.put(4, 4)
obj.get(1)
obj.get(3)
obj.get(4)