-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path146 LRU Cache.py
120 lines (96 loc) · 2.77 KB
/
146 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
class LRUCache(object):
class LL_Node:
def __init__(self, key, val = 0, next = None, prev = None):
self.key = key
self.val = val
self.next = next
self.prev = prev
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity = capacity
self.Node_dict = {}
self.tail = self.LL_Node('tail')
self.head = self.LL_Node('head')
self.head.next = self.tail
self.tail.prev = self.head
self.LL_len = 0
def get(self, key):
"""
:type key: int
:rtype: intw
"""
cur_dict = self.Node_dict
if key in self.Node_dict:
print(key)
cur_node = self.Node_dict[key]
cur_prev = cur_node.prev
cur_next = cur_node.next
cur_prev.next = cur_next
cur_next.prev = cur_prev
head_next = self.head.next
self.head.next = cur_node
cur_node.next = head_next
head_next.prev = cur_node
cur_node.prev = self.head
return cur_node.val
else:
print(-1)
return -1
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: None
"""
cur_dict = self.Node_dict
if key in self.Node_dict:
cur_node = self.Node_dict[key]
cur_node.val = value
self.get(key)
else:
if self.LL_len >= self.capacity:
drop_key = self.tail.prev.key
last_prev = self.tail.prev.prev
last_prev.next = self.tail
self.tail.prev = last_prev
self.Node_dict.pop(drop_key)
self.LL_len -= 1
cur_node = self.LL_Node(key, value)
self.Node_dict[key] = cur_node
head_next = self.head.next
self.head.next = cur_node
cur_node.next = head_next
head_next.prev = cur_node
cur_node.prev = self.head
self.LL_len += 1
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
def test1():
obj = LRUCache(2)
param_1 = obj.get(1)
obj.put(1,1)
obj.put(2,2)
param_1 = obj.get(1)
obj.put(3,3)
param_1 = obj.get(2)
obj.put(4,4)
param_1 = obj.get(1)
param_1 = obj.get(3)
param_1 = obj.get(4)
def test2():
obj = LRUCache(2)
param_1 = obj.get(1)
obj.put(2,1)
obj.put(2,2)
param_1 = obj.get(2)
obj.put(3,3)
param_1 = obj.get(2)
obj.put(4,4)
param_1 = obj.get(1)
param_1 = obj.get(3)
param_1 = obj.get(4)
test2()