Back to problems Solve on LeetCode →

LRU Cache

LeetCode 146 • Medium • Design

Design a cache with get and put in O(1). Evict least recently used when full.
Input: capacity=2, put(1,1), put(2,2), get(1)=1, put(3,3), get(2)=-1, put(4,4), get(1)=-1, get(3)=3, get(4)=4

GetO(1)hash lookup
PutO(1)hash + DLL add/remove
SpaceO(capacity)
Step0/10
headtail (empty)
Ready
Press Play or Step. Hash map + doubly linked list. Head = MRU, tail = LRU. Move to head on access; evict from tail when full.