LRU Cache

Design a data structure for a Least Recently Used (LRU) cache that performs get and put in O(1) average time. Implement the Solution class:

  • Solution(capacity) creates a cache that holds at most capacity distinct keys. The constructor is the first operation and does not count toward the call limit.
  • get(key) returns the value stored for key, or -1 when key is absent. A successful get makes key the most recently used entry.
  • put(key, value) stores value under key and makes key the most recently used entry. If key is already present, update its value in place instead of adding a second entry. If the cache already holds capacity distinct keys, remove the least recently used key before inserting a new one.

Every operation is one method call on a single Solution instance. The reported output is null for the constructor and for every put, and the returned integer for every get.

Examples
Input: [["Solution","put","put","get"],[[2],[1,1],[2,2],[1]]]
Output: [null,null,null,1]
Hints

LRU Cache

Design a data structure for a Least Recently Used (LRU) cache that performs `get` and `put` in O(1) average time. Implement the `Solution` class: