A fixed-size cache with constant-time get and put that evicts the least recently used key. A hash map finds the node, and a linked list keeps the order.
▼The problem
LeetCode 146 (Medium). Design a cache with a fixed capacity where get(key) and put(key, value) both run in O(1). get returns the value (or -1 if the key is missing); when a put takes the cache over capacity, the least recently used key is evicted. Every get or put of a key makes it the most recently used.
Example (capacity 3), each step's shelf order listed most recent first:
TRY IT ON LEETCODE ▶The solution
class Node:
def __init__(self, key=0, val=0):
self.key, self.val = key, val
class LRUCache:
def __init__(self, cap):
self.cap, self.map = cap, {}
self.head, self.tail = Node(), Node()
self.head.next = self.tail
self.tail.prev = self.head
def unlink(self, n):
n.prev.next, n.next.prev = n.next, n.prev
def to_front(self, n):
n.prev, n.next = self.head, self.head.next
self.head.next.prev = n
self.head.next = n
def get(self, key):
if key not in self.map: return -1
n = self.map[key]
self.unlink(n); self.to_front(n)
return n.val
def put(self, key, val):
if key in self.map: self.unlink(self.map[key])
n = self.map[key] = Node(key, val)
self.to_front(n)
if len(self.map) > self.cap:
lru = self.tail.prev
self.unlink(lru)
del self.map[lru.key]Transcript
LRU Cache. Design a cache with a fixed capacity. Get returns a key's value, or minus one if it's missing. Put stores a value under a key. Both must run in constant time, and when the cache is full, a put evicts the least recently used key.
Picture a shelf with three slots: most recently used on the left, least recently used on the right. Every get or put moves its key to the front. So when a new key arrives and there's no room, the one on the right falls off.
The simple way keeps one list in order of use. But finding a key means a scan, and moving it to the front shifts everything ahead of it. That's order n work on every call.
The fix pairs two structures. A hash map takes each key straight to its node, no scan. A doubly linked list keeps the order of use: each node points to the one before it and the one after it. To move a node, join its two neighbors, then splice it in at the front. A few pointer changes, no shifting.
Let's play it with capacity three. Put A, seven. Put B, three. Put C, nine. The shelf is full. Get A returns seven, and A jumps to the front. Now put D, five. That's one too many, so the last item, B, is evicted, and its key is deleted from the map. Without that get, A would have gone. Get B misses: minus one.
In code, sentinel nodes, head and tail, guard the ends, so there are no special cases. Get looks up the node, moves it to the front, and returns its value. Put links a new node at the front; when there are too many keys, it unlinks the node before the tail and deletes its key.
Each call is one hash lookup and a few pointer changes, so get and put are order one. Space is order capacity.
Look it up, move it up, drop the last. That's LRU Cache.