-
Notifications
You must be signed in to change notification settings - Fork 0
/
146LRUCache.py
94 lines (83 loc) · 2.3 KB
/
146LRUCache.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class linkedNode:
def __init__(self, key = 0, val = 0):
self.key = key
self.val = val
self.next = None
self.prev = None
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity = capacity
self.hashmap = dict()
self.head = linkedNode(0, 0)
self.tail = linkedNode(0, 0)
self.head.next = self.tail
self.tail.prev = self.head
self.size = 0
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key not in self.hashmap:
return -1
node = self.hashmap[key]
prev = node.prev
next = node.next
prev.next = next
next.prev = prev
self._insert(node)
return node.val
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
if self.size == self.capacity and key not in self.hashmap:
# delete tail
self._delete()
if key not in self.hashmap:
node = linkedNode(key, value)
self.hashmap[key] = node
self._insert(node)
self.size += 1
else:
node = self.hashmap[key]
node.val = value
prev = node.prev
next = node.next
prev.next = next
next.prev = prev
self._insert(node)
return None
def _delete(self):
# delete least visited element
toDelete = self.tail.prev
key = toDelete.key
tmp = toDelete.prev
tmp.next = self.tail
self.tail.prev = tmp
del self.hashmap[key]
self.size -= 1
def _insert(self, node):
tmp = self.head.next
self.head.next = node
node.next = tmp
tmp.prev = node
node.prev = self.head
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
lRUCache = LRUCache(2)
print(lRUCache.get(2))
print(lRUCache.put(2, 6))
print(lRUCache.get(1))
print(lRUCache.put(1, 5))
print(lRUCache.put(1, 2))
print(lRUCache.get(1))
print(lRUCache.get(2))
# [null,-1,null,-1,null,null,2,6]