-
Notifications
You must be signed in to change notification settings - Fork 0
/
146.lru-cache.py
85 lines (74 loc) · 2.13 KB
/
146.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
#
# @lc app=leetcode id=146 lang=python3
#
# [146] LRU Cache
#
# @lc code=start
class Node:
def __init__(self, key: int, val: int):
self.key = key
self.val = val
self.prev = None
self.next = None
class LRUCache:
def __init__(self, capacity: int):
self.capacity = capacity
self.cache = dict()
self.head = None
self.tail = None
def get(self, key: int) -> int:
if key in self.cache:
node = self.cache[key]
if node != self.head:
self.move_to_top(node)
return node.val
else:
return -1
def put(self, key: int, value: int) -> None:
if key is None or value is None:
return
if key in self.cache:
node = self.cache[key]
if node != self.head:
self.move_to_top(node)
self.cache.pop(key)
self.head.val = value
self.cache[key] = self.head
return
else:
new = Node(key, value)
if self.head:
head = self.head
head.prev = new
new.next = head
else:
self.tail = new
self.head = new
self.cache[key] = new
if len(self.cache) > self.capacity:
self.cache.pop(self.tail.key)
self.tail = self.tail.prev
self.tail.next = None
def print_node(self, current):
elements = []
while current:
temp = f"{current.key}-{current.val}"
elements.append(temp)
current = current.next
linked_list_string = " -> ".join(elements)
def move_to_top(self, node):
if node.prev:
node.prev.next = node.next
if node.next:
node.next.prev = node.prev
else:
self.tail = node.prev
node.next = self.head
node.prev = None
self.head.prev = node
self.head = node
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)
# @lc code=end