Practice LRU Cache
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Implement LRUCache(capacity), get(key), and put(key, value). get returns the stored value or -1 when absent. A successful get and every put make that key most recently used. put updates an existing key or inserts a new one; if insertion exceeds capacity, remove the least recently used key. Both operations must take O(1) average time. Practice approach: Keep a hash map from keys to nodes and a doubly linked list ordered by recency. Move an accessed node to the recent end. Evict from the opposite end and delete that key from the map. Updating a key must not allocate a second list entry.
Source: LRU Cache on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
LRUCache(2); put(1,10); put(2,20); get(1); put(3,30); get(2); get(3)
[null,null,null,10,null,-1,30]
Reading key 1 refreshes it, so inserting key 3 evicts key 2.
Constraints
Related LeetCode Problems
Practice more problems similar to LRU Cache.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.