-
Notifications
You must be signed in to change notification settings - Fork 2
/
sol.py
63 lines (52 loc) · 1.54 KB
/
sol.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
from typing import List
class Node:
def __init__(self, key, dataval=None):
self.key = key
self.val = dataval
self.next = None
self.prev = None
class LRUCache:
def __init__(self, capacity: int):
self.cap = capacity
self.size = 0
self.head = Node(None)
self.last = Node(None)
self.head.next = self.last
self.last.prev = self.head
self.cache = {}
def get(self, key: int) -> int:
if key not in self.cache:
return -1
node = self.cache[key]
self.extract(node)
self.attachToStart(node)
return node.val
def put(self, key: int, value: int) -> None:
if key in self.cache:
node = self.cache[key]
node.val = value
self.extract(node)
self.attachToStart(node)
return
node = Node(key, value)
self.cache[key] = node
self.attachToStart(node)
self.size += 1
if self.size > self.cap:
self.removeLast()
self.size -= 1
def removeLast(self) -> None:
node = self.last.prev
self.extract(node)
del self.cache[node.key]
def attachToStart(self, node) -> None:
nextNode = self.head.next
self.head.next = node
node.prev = self.head
node.next = nextNode
nextNode.prev = node
def extract(self, node) -> None:
before = node.prev
after = node.next
before.next = after
after.prev = before