Skip to content

Commit 34823a4

Browse files
author
Partho Biswas
committed
146. LRU Cache
1 parent b91993a commit 34823a4

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ I have solved quite a number of problems from several topics. See the below tabl
221221
|20| [160. Intersection of Two Linked Lists](https://tinyurl.com/rz6nrop)| [Python](https://tinyurl.com/wu6rdaw/160_Intersection_of_Tw_Linked_Lists.py)| [Art 1](https://tinyurl.com/urrs7uy), [Art 2](https://tinyurl.com/wkya7ks) | Easy | --- |
222222
|21| **[138. Copy List with Random Pointer](https://tinyurl.com/uhaw95f)** | [Python](https://tinyurl.com/wu6rdaw/138_Copy_List_with_Random_Pointer.py)| **[Vid 1](https://tinyurl.com/reaqam9), [Art 1](https://tinyurl.com/tnwofvs)** | Medium | **TODO: Check again. Very important. Learned a lot of things** |
223223
|22| **[430. Flatten a Multilevel Doubly Linked List](https://tinyurl.com/sgpamoh)** | [Python](https://tinyurl.com/wu6rdaw/430_Flatten_a_Multilevel_Doubly_Linked_List.py)| [codinginterviewclass.com](https://tinyurl.com/wtmsrqa), **[Vid 1](https://tinyurl.com/tbp99p2), [Art 1](https://tinyurl.com/un8p7f2)** | Medium | **TODO: Check again. Very important. Learned a lot of things** |
224+
|23| **[146. LRU Cache](https://tinyurl.com/zu2qbfl)** | [Python](https://tinyurl.com/wu6rdaw/146_LRU_Cache.py)| **[Vid 1](https://tinyurl.com/rtnhhys), [backtobackswe.com](https://tinyurl.com/vbpvjdc), [algoexpert.io](https://tinyurl.com/t2uhpgn)** | Medium | **TODO: Check again. Very important. Learned a lot of things** |
224225

225226

226227
</p>

leetcode.com/python/146_LRU_Cache.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Approach 2: Hashmap + DoubleLinkedList
2+
class LRUCache(object):
3+
def __init__(self, capacity):
4+
self.cache = {}
5+
self.capacity = capacity or 1
6+
self.currentCapacity = 0
7+
self.listOfMostRecent = DoublyLinkedList()
8+
9+
10+
# O(1) time | O(1) space
11+
def put(self, key, value):
12+
if key not in self.cache:
13+
if self.currentCapacity == self.capacity:
14+
self.evictLeastRecent()
15+
else:
16+
self.currentCapacity += 1
17+
self.cache[key] = DoublyLinkedListNode(key, value)
18+
else:
19+
self.replaceKey(key, value)
20+
self.updateMostRecent(self.cache[key])
21+
22+
23+
# O(1) time | O(1) space
24+
def get(self, key):
25+
if key not in self.cache:
26+
return None
27+
self.updateMostRecent(self.cache[key])
28+
return self.cache[key].value
29+
30+
31+
# O(1) time | O(1) space
32+
def getMostRecentKey(self):
33+
return self.listOfMostRecent.head.key
34+
35+
36+
def evictLeastRecent(self):
37+
keyToRemove = self.listOfMostRecent.tail.key
38+
self.listOfMostRecent.removeTail()
39+
del self.cache[keyToRemove]
40+
41+
42+
def updateMostRecent(self, node):
43+
self.listOfMostRecent.setToHead(node)
44+
45+
46+
def replaceKey(self, key, value):
47+
if key not in self.cache:
48+
raise Exception("The provided key isn't in the cache!")
49+
self.cache[key].value = value
50+
51+
52+
53+
54+
class DoublyLinkedList:
55+
def __init__(self):
56+
self.head = None
57+
self.tail = None
58+
59+
60+
def setToHead(self, node):
61+
if self.head == node:
62+
return
63+
elif self.head is None:
64+
self.head = node
65+
self.tail = node
66+
elif self.head == self.tail:
67+
self.tail.prev = node
68+
self.head = node
69+
self.head.next = self.tail
70+
else:
71+
if self.tail == node:
72+
self.removeTail()
73+
node.removeBindings()
74+
self.head.prev = node
75+
node.next = self.head
76+
self.head = node
77+
78+
79+
def removeTail(self):
80+
if self.tail is None:
81+
return
82+
if self.tail == self.head:
83+
self.head = None
84+
self.tail = None
85+
return
86+
self.tail = self.tail.prev
87+
self.tail.next = None
88+
89+
90+
class DoublyLinkedListNode:
91+
def __init__(self, key, value):
92+
self.key = key
93+
self.value = value
94+
self.prev = None
95+
self.next = None
96+
97+
98+
def removeBindings(self):
99+
if self.prev is not None:
100+
self.prev.next = self.next
101+
if self.next is not None:
102+
self.next.prev = self.prev
103+
self.prev = None
104+
self.next = None

0 commit comments

Comments
 (0)