Leetcode 146. LRU Cache
Explanation for Leetcode 146 - LRU Cache, and its solution in Python.
Problem
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
Explanation
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // cache is {1=1}
lRUCache.put(2, 2); // cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
lRUCache.get(2); // returns -1 (not found)
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
lRUCache.get(1); // return -1 (not found)
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Approach
Here is the Python code for the solution:
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
class Node:
def __init__(self, key, val):
self.key = key
self.val = val
self.prev = self.next = None
class LRUCache:
# initialize variables
def __init__(self, capacity:int):
self.cap = capacity
self.cache = {}
self.left, self.right = Node(0, 0), Node(0, 0)
self.left.next = self.right
self.right.prev = self.left
# removing node
def remove(self, node):
prev, nxt = node.prev, node.next
prev.next, nxt.prev = nxt, prev
# inserting node to right, since it's recently used
def insert(self, node):
prev, nxt = self.right.prev, self.right
prev.next = nxt.prev = node
node.next, node.prev = nxt, prev
# get value using key in dict, update the linked list
def get(self, key: int):
if key in self.cache:
self.remove(self.cache[key])
self.insert(self.cache[key])
return self.cache[key].val
return -1
# put value in node, and dict update the linked list
def put(self, key: int, val: int):
if key in self.cache:
self.remove(self.cache[key])
self.cache[key] = Node(key, val)
self.insert(self.cahce[key])
if len(self.cache) > self.cap:
lru = self.left.next
self.remove(lru)
del self.cache[lru.key]
Time Complexity and Space Complexity
Time Complexity: $O(1)$ for all operations
Space Complexity: $O(n)$
This post is licensed under CC BY 4.0 by the author.