-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146. LRU Cache.py
More file actions
52 lines (39 loc) · 1.3 KB
/
Copy path146. LRU Cache.py
File metadata and controls
52 lines (39 loc) · 1.3 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
class Node:
def __init__(self,key = 0, val = 0):
self.key = key
self.val = val
self.next = self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.hm = dict()
self.Left, self.Right = Node(), Node()
self.Left.next = self.Right
self.Right.prev = self.Left
def remove(self,node):
prev, nxt = node.prev, node.next
prev.next = nxt
nxt.prev = prev
def insert(self,node):
prev = self.Right.prev
prev.next = node
node.next = self.Right
node.prev = prev
self.Right.prev = node
def get(self, key: int) -> int:
if key not in self.hm: return -1
self.remove(self.hm[key])
self.insert(self.hm[key])
return self.hm[key].val
def put(self, key: int, value: int) -> None:
if key in self.hm: self.remove(self.hm[key])
self.hm[key] = Node(key,value)
self.insert(self.hm[key])
if len(self.hm) > self.capacity:
lru = self.Left.next
self.remove(lru)
del self.hm[lru.key]
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)