-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlru-cache.py
240 lines (225 loc) · 6.45 KB
/
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# LRU Cache
# 🟣 Very Hard
#
# https://www.algoexpert.io/questions/lru-cache
#
# Tags: Linked List - Hash Map
import timeit
from utils.linked_list import LinkedList
# Do not edit the class below except for the insertKeyValuePair,
# getValueFromKey, and getMostRecentKey methods. Feel free
# to add new properties and methods to the class.
class LRUCache:
def __init__(self, maxSize):
self.maxSize = maxSize or 1
self.d = {}
self.head = self.tail = None
def insertKeyValuePair(self, key, value):
# If the node exists already.
if key in self.d:
nodeToUpdate = self.d[key]
nodeToUpdate.val = value
self.touch(nodeToUpdate)
return
# This is a new insert.
nodeToInsert = DLNode(key, value)
if not self.head:
self.head = self.tail = nodeToInsert
self.d[nodeToInsert.key] = nodeToInsert
return
if len(self.d) == self.maxSize:
nodeToRemove = self.tail
if self.maxSize == 1:
self.head = self.tail = nodeToInsert
else:
nodeToInsert.next = self.head
self.head.prev = nodeToInsert
self.head = nodeToInsert
newTail = nodeToRemove.prev
newTail.next = None
self.tail = newTail
self.d.pop(nodeToRemove.key)
else:
nodeToInsert.next = self.head
self.head.prev = nodeToInsert
self.head = nodeToInsert
self.d[nodeToInsert.key] = nodeToInsert
def getValueFromKey(self, key):
# Write your code here.
if key not in self.d:
return None
node = self.d[key]
self.touch(node)
return node.val
def getMostRecentKey(self):
# Write your code here.
if self.head:
return self.head.key
return None
def touch(self, node):
if node is self.head:
return
p, n = node.prev, node.next
p.next = n
if n:
n.prev = p
node.prev = None
node.next = self.head
self.head.prev = node
self.head = node
if self.tail is node:
self.tail = p
class DLNode:
def __init__(self, key, val, prev=None, next=None):
self.key = key
self.val = val
self.prev = prev
self.next = next
def __repr__(self):
return f"ListNode({self.key}:{self.val})"
def test():
executors = [
LRUCache,
]
tests = [
[
[
"LRUCache",
"insertKeyValuePair",
"insertKeyValuePair",
"insertKeyValuePair",
"getMostRecentKey",
"getValueFromKey",
"getMostRecentKey",
"insertKeyValuePair",
"getValueFromKey",
"getMostRecentKey",
"insertKeyValuePair",
"getMostRecentKey",
"getValueFromKey",
],
[
[3],
["b", 2],
["a", 1],
["c", 3],
None,
["a"],
None,
["d", 4],
["b"],
None,
["a", 5],
None,
["a"],
],
[
None,
None,
None,
None,
"c",
1,
"a",
None,
None,
"d",
None,
"a",
5,
],
],
[
[
"LRUCache",
"insertKeyValuePair",
"insertKeyValuePair",
"insertKeyValuePair",
"insertKeyValuePair",
"getValueFromKey",
"getMostRecentKey",
"insertKeyValuePair",
"getValueFromKey",
"getValueFromKey",
"getValueFromKey",
"insertKeyValuePair",
"getValueFromKey",
"getValueFromKey",
"insertKeyValuePair",
"getValueFromKey",
"getValueFromKey",
"getValueFromKey",
"getValueFromKey",
"getValueFromKey",
],
[
[4],
["a", 1],
["b", 2],
["c", 3],
["d", 4],
["a"],
None,
["e", 5],
["a"],
["b"],
["c"],
["f", 5],
["c"],
["d"],
["g", 5],
["e"],
["a"],
["c"],
["f"],
["g"],
],
[
None,
None,
None,
None,
None,
1,
"a",
None,
1,
None,
3,
None,
3,
None,
None,
None,
1,
3,
5,
5,
],
],
]
for executor in executors:
start = timeit.default_timer()
for _ in range(1):
for col, t in enumerate(tests):
# The capacity comes wrapped in an array, unwrap it.
sol = executor(t[1][0][0])
for i in range(1, len(t[0])):
call = t[0][i]
if call == "getValueFromKey":
result = getattr(sol, call)(t[1][i][0])
elif call == "getMostRecentKey":
result = getattr(sol, call)()
else:
result = getattr(sol, call)(t[1][i][0], t[1][i][1])
exp = t[2][i]
assert result == exp, (
f"\033[93m» {result} <> {exp}\033[91m for"
+ f" test {col} using \033[1m{executor.__name__}"
)
stop = timeit.default_timer()
used = str(round(stop - start, 5))
cols = "{0:20}{1:10}{2:10}"
res = cols.format(executor.__name__, used, "seconds")
print(f"\033[92m» {res}\033[0m")
test()