cheetcode

43. LRU CacheMedium

Design a cache with O(1) get and put plus least-recent eviction.

Implement an LRUCache with a fixed positive capacity. get returns the value for a key if it exists, otherwise -1.

put inserts or updates a key. If the cache grows past capacity, evict the least recently used key.

Both get and put make a key recently used, so every successful access affects the future eviction order.

Examples

Input: operations = ["LRUCache","put","put","get","put","get","put","get","get","get"], args = [[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]
Input: operations = ["LRUCache","put","get"], args = [[1],[2,1],[2]]
Output: [null,null,1]

Constraints

  • 1 <= capacity <= 3,000
  • 0 <= key <= 10,000
  • 0 <= value <= 100,000
  • At most 200,000 calls will be made to get and put.