Course · Section 9: Data Structures, Modern Operators and Strings · Lecture 124: Maps: Fundamentals
Design a Least-Recently-Used (LRU) cache with a fixed capacity. Implement `LRUCache`: - `new LRUCache(capacity)` creates a cache of the given capacity. - `get(key)` returns the value, or `-1` if absent; using a key counts as a use. - `put(key, value)` inserts or updates; if over capacity, evict the least recently used key.
+ 1 hidden test run on Submit.
A Map keeps keys in insertion order, so re-inserting on access marks recency and the first key is always the least recently used. All operations are O(1) average. O(capacity) space.
Run your code to see results.