← All problems
LRU Cache
HardHash Map + Doubly Linked List
Implement an LRU cache with O(1) get and put. The interface is: `LRUCache(capacity)`, `get(key)` returns value or -1, `put(key, value)` inserts or updates and evicts least-recently-used if over capacity.
Examples
Input: LRUCache(2); put(1,1); put(2,2); get(1); put(3,3); get(2); put(4,4); get(1); get(3); get(4)
Output: [1, -1, -1, 3, 4]
Hints
Solution
Language:
Loading editor…
Output0 lines
Run your code to see output here.
Click Run all tests to check your solution against 2 test cases.