← Practice · Linked list

LRU Cache Medium

Design a class LRUCache with a fixed capacity. - constructor(capacity) - get(key): return the value, or -1 if the key is absent. A read counts as a use. - put(key, value): insert or update the key. If the cache is over capacity afterwards, evict the least recently used key. Both operations should run in O(1) average time.

Examples

  • capacity 2; put(1,1), put(2,2), get(1) -> 1, put(3,3) evicts key 2, get(2) -> -1

Define a class named LRUCache in JavaScript.

00:00

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests