LRU Cache
Implement lruCacheGetResults
Design a Least Recently Used (LRU) cache that supports get(key) and put(key, value), each in O(1) time on average, with a fixed capacity — once the cache is full, inserting a new key evicts whichever key was used least recently.
To make this testable with a single function call, this version takes a capacity and a fixed sequence of operations (each either "get" or "put") with their arguments, replays them in order, and returns the result of every get call as an array (put has no return value in the real API).
Example 1:
Input: capacity = 2, ops = ["put","put","put","get","get","put","get"], args = [[3,30],[7,70],[3,99],[7,0],[3,0],[5,50],[7,0]]
Output: [70,99,-1]
Example 2:
Input: capacity = 1, ops = ["put","get","put","get","get"], args = [[1,100],[1,0],[2,200],[1,0],[2,0]]
Output: [100,-1,200]
Example 3:
Input: capacity = 3, ops = ["put","put","put","get","put","get","get"], args = [[1,1],[2,2],[3,3],[2,0],[2,99],[1,0],[2,0]]
Output: [2,1,99]
+ 6 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ capacity ≤ 5 - ●
1 ≤ number of operations ≤ 10 - ●
Each operation is "get" or "put" - ●
0 ≤ key, value ≤ 200 - ●
This version replays a fixed sequence of operations and reports the result of every get call, in order (put has no return value in the real API, so only get results are collected)
capacity =
2
ops =
["put", "put", "put", "get", "get", "put", "get"]
args =
[[3,30], [7,70], [3,99], [7,0], [3,0], [5,50], [7,0]]