-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterviewbit-heaps-and-maps-lru-cache.py
69 lines (63 loc) · 2.36 KB
/
interviewbit-heaps-and-maps-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
58
59
60
61
62
63
64
65
66
67
68
69
class LRUCache:
class DLLNode:
def __init__(self, val=None):
self.val = val
self.prev = None
self.next = None
class DLL:
def __init__(self):
self.head = None
self.tail = None
def update_index(self, the_key):
if self.cache[the_key][1] != self.index.tail:
if self.cache[the_key][1] == self.index.head:
self.index.head = self.cache[the_key][1].next
self.index.head.prev = None
else:
self.cache[the_key][1].prev.next = self.cache[the_key][1].next
self.cache[the_key][1].next.prev = self.cache[the_key][1].prev
self.index.tail.next = self.cache[the_key][1]
self.cache[the_key][1].prev = self.index.tail
self.index.tail = self.cache[the_key][1]
self.cache[the_key][1].next = None
# @param capacity, an integer
def __init__(self, capacity):
self.cache = {}
self.capacity = capacity
self.index = self.DLL()
# @return an integer
def get(self, key):
if key in self.cache:
self.update_index(key)
return self.cache[key][0]
else:
return -1
# @param key, an integer
# @param value, an integer
# @return nothing
def set(self, key, value):
if key in self.cache:
self.cache[key][0] = value
self.update_index(key)
else:
new_node = self.DLLNode(key)
if len(self.cache) < self.capacity:
if not self.index.head:
self.index.head = new_node
self.index.tail = new_node
else:
self.index.tail.next = new_node
new_node.prev = self.index.tail
self.index.tail = new_node
else:
del self.cache[self.index.head.val]
if self.capacity == 1:
self.index.head = new_node
self.index.tail = new_node
else:
self.index.head = self.index.head.next
self.index.head.prev = None
new_node.prev = self.index.tail
self.index.tail.next = new_node
self.index.tail = new_node
self.cache[key] = [value, new_node]