LRU Cache

Medium Solved

Description

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

Implement the LRUCache class:

  • LRUCache(int capacity)
  • int get(int key)
  • void put(int key, int value)

Input format:

  • Line 1: Commands array (JSON)
  • Line 2: Arguments array (JSON)

Example

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]

Note:

Print output as a JSON array.

No submissions yet.

Discuss hash map + doubly linked list, eviction strategy, and time complexity.

Test Cases