Problem · Design
LRU Cache Operations
Learn this problemProblem statement
Process a sequence of operations on a least recently used (LRU) cache with a fixed positive capacity.
The cache starts empty. The parallel arrays operations and arguments describe the calls in order:
gethas arguments[key]. Return the stored value, or-1when the key is absent. A successfulgetmakes that key the most recently used.puthas arguments[key, value]. Insert or update the key and make it the most recently used. If inserting a new key would exceedcapacity, first evict the least recently used key.
Return an array containing the results of the get operations, in operation order. A put operation produces no result.
Function
runLruCache(capacity: int, operations: String[], arguments: int[][]) → int[]Examples
Example 1
capacity = 2operations = ["put","put","get","put","get","get"]arguments = [[1,1],[2,2],[1],[3,3],[2],[3]]return = [1,-1,3]Reading key 1 makes it most recent. Inserting key 3 then evicts key 2, so the final two reads return -1 and 3.
Example 2
capacity = 1operations = ["put","put","get","put","get"]arguments = [[5,10],[5,20],[5],[6,30],[5]]return = [20,-1]Updating key 5 changes its value without increasing the cache size. Inserting key 6 later evicts key 5 because the capacity is 1.
Constraints
1 <= capacity <= 10^51 <= operations.length <= 2 * 10^5operations.length == arguments.length- Each operation is exactly
getorput. - A
getrow contains exactly one integer, and aputrow contains exactly two integers. 0 <= key <= 10^90 <= value <= 10^9