-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path146.lru-cache.py
More file actions
78 lines (66 loc) · 2.31 KB
/
146.lru-cache.py
File metadata and controls
78 lines (66 loc) · 2.31 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
77
78
from typing import Optional
# @leet start
class Node:
def __init__(
self,
key: int,
val: int,
next: "Optional[Node]" = None,
prev: "Optional[Node]" = None,
):
self.key = key
self.val = val
self.next = next
self.prev = prev
def __str__(self) -> str:
return f"{self.key, self.val}"
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache: dict[int, Node] = {}
self.head = Node(-1, 0)
self.tail = Node(99, 99)
self.head.next = self.tail
self.tail.prev = self.head
self.length = 0
def _insert_node(self, node: Node, new_node: Node):
new_node.next, new_node.prev = node.next, node
node.next.prev = new_node
node.next = new_node
print(f"Insert node, Prev node: {new_node.prev}")
print(f"Insert node, Next node: {new_node.next}")
def _remove_node(self, node: Node):
node.prev.next = node.next
node.next.prev = node.prev
print(f"Remove node, Prev node: {node.prev}")
print(f"Remove node, Next node: {node.next}")
def get(self, key: int) -> int:
if key in self.cache:
print(f"Moving up in cache, {self.cache[key]}")
self._remove_node(self.cache[key])
self._insert_node(self.head, self.cache[key])
print(f"VALUE TO BE RETURNED: {self.cache[key].val}")
return self.cache[key].val
return -1
def put(self, key: int, value: int) -> None:
print(f"{self.length = }")
if key in self.cache:
print(f"Updating {self.cache[key]}")
self._remove_node(self.cache[key])
self.length -= 1
if self.length >= self.capacity:
print("Capacity reached")
print(f"Deleting: {self.tail.prev}")
self.cache.pop(self.tail.prev.key)
self._remove_node(self.tail.prev)
self.length -= 1
new_node = Node(key, value)
self.cache[key] = new_node
print(f"Inserted: { key = }, {value = }")
self._insert_node(self.head, new_node)
self.length += 1
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
# @leet end