-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru_cache.py
More file actions
76 lines (56 loc) · 1.56 KB
/
lru_cache.py
File metadata and controls
76 lines (56 loc) · 1.56 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
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
from typing import Dict, Union
class Node:
def __init__(self, key: int, value: int):
self.previous: Union[Node, None] = None
self.next: Union[Node, None] = None
self.key = key
self.value = value
class Queue:
def __init__(self):
self.head: Union[Node, None] = None
self.tail: Union[Node, None] = None
def add(self, key: int, value: int) -> Node:
node = Node(key, value)
if self.head is None:
self.head = node
self.tail = self.head
return node
node.previous = self.tail
self.tail.next = node
self.tail = self.tail.next
return node
def remove(self, node: Node) -> None:
if not node:
return
if self.head == node:
self.head = self.head.next
if self.head:
self.head.previous = None
return
if self.tail == node:
self.tail = self.tail.previous
self.tail.next = None
return
node.previous.next = node.next
node.next.previous = node.previous
node = None
class LRUCache:
def __init__(self, capacity: int):
self.data: Dict[int, Node] = {}
self.capacity = capacity
self.queue = Queue()
def get(self, key: int) -> int:
if key not in self.data:
return -1
value = self.data[key].value
self.queue.remove(self.data[key])
self.data[key] = self.queue.add(key, value)
return value
def put(self, key: int, value: int) -> None:
if key not in self.data and len(self.data) == self.capacity:
removed_key = self.queue.head.key
self.queue.remove(self.queue.head)
del self.data[removed_key]
if key in self.data:
self.queue.remove(self.data[key])
self.data[key] = self.queue.add(key, value)