133. LRU Cache

MediumDesign

Design a data structure that follows the constraints of a Least Recently Used (LRU) cache. Implement the LRUCache class with a given capacity. The get method should return the value of a key if it exists and mark it as recently used, otherwise return -1. The put method should update or insert the key-value pair. If the cache exceeds its capacity, evict the least recently used key. Both operations must run in O(1) average time.

Examples

Input: ["LRUCache","put","put","get","put","get","put","get","get","get"] [[2],[1,1],[2,2],[1],[3,3],[2],[4,4],[1],[3],[4]]

Output: [null,null,null,1,null,-1,null,-1,3,4]

Explanation: Public test case for LRU Cache

Constraints

  • 1 <= capacity <= 3000
  • 0 <= key <= 10^4
  • 0 <= value <= 10^5
Loading...

Run checks all cases above. Submit evaluates all test cases.