LRU Cache Design

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.

Implement the LRUCache class:

  • LRUCache(capacity) - Initialize with the capacity of the LRU cache
  • get(key) - Return the value if the key exists, otherwise return -1
  • put(key, value) - Update or insert the value. If capacity is exceeded, evict the least recently used item.

Both get and put operations must run in O(1) time complexity.

Examples
Input: [["LRUCache","put","put","get","get","get"],[[],[1,1],[2,2],[1],[2],[3]]]
Output: [null,null,null,1,2,-1]
Hints

LRU Cache Design

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.