forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru-cache.py
More file actions
47 lines (40 loc) · 1.27 KB
/
lru-cache.py
File metadata and controls
47 lines (40 loc) · 1.27 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
class Node:
def __init__(self, key: int, val: int):
self.key = key
self.val = val
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.dic = {} #[0]
self.head = Node(0, 0) #[3]
self.tail = Node(0, 0) #[3]
self.head.next = self.tail
self.tail.prev = self.head
def remove(self, node):
node.prev.next = node.next
node.next.prev = node.prev
def promote(self, node): #[1]
#set the node next to head
temp = self.head.next
node.next = temp
temp.prev = node
self.head.next = node
node.prev = self.head
def get(self, key: int) -> int:
if key in self.dic:
node = self.dic[key]
self.remove(node)
self.promote(node)
return node.val
return -1
def put(self, key: int, value: int) -> None:
if key in self.dic:
self.remove(self.dic[key])
node = Node(key, value)
self.promote(node)
self.dic[key] = node
if len(self.dic)>self.capacity: #[2]
del self.dic[self.tail.prev.key]
self.remove(self.tail.prev)