-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru-cache.py
More file actions
34 lines (31 loc) · 1.04 KB
/
lru-cache.py
File metadata and controls
34 lines (31 loc) · 1.04 KB
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
class LRUCache:
def __init__(self, capacity: int):
self.recents = deque([])
self.cap = capacity
self.count = 0
self.cache = {}
def get(self, key: int) -> int:
if key not in self.cache:
return -1
num = self.cache[key]
if self.recents and self.recents[-1] != key:
self.recents.remove(key)
self.recents.append(key)
return num
def put(self, key: int, value: int) -> None:
if key in self.cache:
self.cache[key] = value
self.recents.remove(key)
self.recents.append(key)
else:
if self.count < self.cap:
self.cache[key] = value
self.count += 1
self.recents.append(key)
else:
least = self.recents.popleft()
while least not in self.cache:
least = self.recents.popleft()
del self.cache[least]
self.cache[key] = value
self.recents.append(key)