-
Notifications
You must be signed in to change notification settings - Fork 24
/
146 - LRU Cache.py
99 lines (87 loc) · 2.89 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Solution: Implement a doubly linked list and a hashtable to get both operations in O(1).
# Note: In Python we could use an OrderedDict to solve the question, but
# this defeats the purpose of the question since an OrderedDict is basically an LRU cache
# under the hood. We should implement our own solution instead.
# Tip #1: Use a tail pointer instead of creating cycles in the DLL
# (i.e don't use head.prev to get tail)
# Tip #2: Remember to remove ALL pointers (including those on the node itself) when you
# remove a node
# Node is an object in a DLL
class Node(object):
def __init__(self,key,value):
self.key = key
self.value = value
self.next = None
self.prev = None
class LRUCache(object):
def __init__(self, capacity):
"""
:type capacity: int
"""
self.capacity = capacity
self.cache = {}
self.head = None
self.tail = None
# Adds a node to the front of the DLL
def addNode(self, node):
if self.head is None:
self.head = node
else:
node.next = self.head
self.head.prev = node
self.head = node
if self.tail is None:
self.tail = node
# Removes a node from the DLL
def removeNode(self, node):
if node.next is not None:
node.next.prev = node.prev
if node.prev is not None:
node.prev.next = node.next
if self.head == node:
self.head = node.next
if self.tail == node:
self.tail = node.prev
# Gotcha: Need to remove the pointers on the node itself in case you add it back in
node.next = None
node.prev = None
def get(self, key):
"""
:type key: int
:rtype: int
"""
if key in self.cache:
node = self.cache[key]
# Move node to front of DLL
self.removeNode(node)
self.addNode(node)
return node.value
else:
return -1
def put(self, key, value):
"""
:type key: int
:type value: int
:rtype: void
"""
if key in self.cache:
# Key is already in cache, just need to update it's value and it to the front
node = self.cache[key]
node.value = value
self.removeNode(node)
self.addNode(node)
else:
# We're adding a new key to the cache
if self.capacity > 0:
self.capacity -=1
else:
# Delete tail of DLL
del self.cache[self.tail.key]
self.removeNode(self.tail)
node = Node(key,value)
self.cache[key] = node
self.addNode(node)
# Your LRUCache object will be instantiated and called as such:
# obj = LRUCache(capacity)
# param_1 = obj.get(key)
# obj.put(key,value)