-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlru-cache.py
250 lines (230 loc) · 7.68 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
241
242
243
244
245
246
247
248
249
250
# 146. LRU Cache
# 🟠 Medium
#
# https://leetcode.com/problems/lru-cache/
#
# Tags: Hash Table - Linked List - Design - Doubly-Linked List
from __future__ import annotations
import timeit
from collections import OrderedDict
# Useful to watch the current state of the cache in the watch tab of the
# debugger using the watch expression `serializeLinkedList(self.lru)`
from data import serializeLinkedList
# A doubly linked list node.
class DListNode:
def __init__(
self,
prev: DListNode = None,
next: DListNode = None,
val: int = 0,
key: int = 0,
):
self.prev = prev
self.next = next
self.val = val
self.key = key
# Design the LRUCache using a dictionary to have O(1) access to any
# element in the cache via the key, and a doubly linked list, to also
# have O(1) access to both ends of the list, the LRU and MRU elements.
# The dictionary uses integers as keys and doubly linked list nodes as
# values. The nodes store their value, the key, to have O(1) removal of
# the LRU, and pointers to the next and previous siblings.
#
# Time complexity: O(1) - For init, get and put methods.
# Space complexity: O(n) - Both the list and the cache will grow
# linearly with the size of the input.
#
# Runtime: 959 ms, faster than 83.51%
# Memory Usage: 75.8 MB, less than 42.75%
class LRUCache:
def __init__(self, capacity: int):
# Initialize the class attributes.
self.capacity = capacity
# A dictionary of key => ListNode.
self.cache = {}
# The least recently used key.
self.lru = None
# The most recently used key.
self.mru = None
def get(self, key: int) -> int:
if key in self.cache:
node: DListNode = self.cache[key]
self.touch(node)
return node.val
else:
# Return -1 for key not found.
return -1
def put(self, key: int, value: int) -> None:
if key in self.cache:
node: DListNode = self.cache[key]
# Update the lru references.
node.val = value
self.touch(node)
# If the key does not exist, add it, it may be necessary to
# remove the lru node.
else:
if len(self.cache) == self.capacity:
# Evict lru key.
del self.cache[self.lru.key]
if self.capacity == 1:
self.lru = None
self.mru = None
else:
self.lru = self.lru.next
self.lru.prev = None
# Now we are guaranteed to have space for one more node.
node = DListNode(prev=self.mru, next=None, val=value, key=key)
self.mru = node
self.cache[key] = node
# If this is the only node in the cache, it is also the lru.
if len(self.cache) == 1:
self.lru = node
# If there are more nodes, update its prev to link to it.
else:
node.prev.next = node
def touch(self, node: DListNode) -> None:
# Update the lru references if the currently accessed node
# is not the most recently used node already.
# If node already is the most recently used node do nothing.
if self.mru == node:
return
if self.lru == node:
# Update end of the list removing this node.
node.next.prev = None
self.lru = node.next
else:
# Remove this node from the list updating its siblings
# pointers.
node.next.prev = node.prev
node.prev.next = node.next
# Update start of the list adding this node as the new mru.
node.prev = self.mru
self.mru.next = node
self.mru = node
# Use collections.OrderedDictionary to implement the LRUCache, we only
# need to worry about checking the size and moving the accessed element
# to the MRU position, the rest of the operations are supported by the
# OrderedDictionary itself.
#
# Time complexity: O(1) - This solution uses the OrderedDictionary
# methods `move_to_end` and `popitem`, both of them are O(1).
# Space complexity: O(n) - The OrderedDictionary will grow to the same
# size as the input.
#
# Runtime: 1419 ms, faster than 40.37%
# Memory Usage: 74.6 MB, less than 94.63%
class OrDictLRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = OrderedDict()
def get(self, key: int) -> int:
if key in self.cache:
self.cache.move_to_end(key)
return self.cache[key]
else:
return -1
def put(self, key: int, value: int) -> None:
if key in self.cache:
self.cache.move_to_end(key)
self.cache[key] = value
else:
self.cache[key] = value
if len(self.cache) > self.capacity:
self.cache.popitem(last=False)
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
def test():
executors = [
LRUCache,
OrDictLRUCache,
]
tests = [
# Edge case with capacity 1. Lru and mru are always the same.
[
[
"LRUCache",
"put",
"put",
"get",
"get",
"put",
"get",
"put",
"get",
"get",
"get",
],
[
[1],
[1, 1],
[2, 2],
[1],
[2],
[3, 3],
[2],
[4, 4],
[1],
[3],
[4],
],
[None, None, None, -1, 2, None, -1, None, -1, -1, 4],
],
# Regular case with capacity > 1.
[
[
"LRUCache",
"put",
"put",
"get",
"put",
"get",
"put",
"get",
"get",
"get",
],
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]],
[None, None, None, 1, None, -1, None, -1, 3, 4],
],
[
[
"LRUCache",
"put",
"put",
"get",
"put",
"get",
"put",
"get",
"get",
"get",
],
[[2], [1, 0], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]],
[None, None, None, 0, None, -1, None, -1, 3, 4],
],
]
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 == "get":
result = getattr(sol, call)(t[1][i][0])
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()