LRU Cache Operation Sequence
Problem statement
Process an ordered sequence of operations on a least recently used cache that starts empty.
[1, key, value]is a put: insert or update the value and make that key most recently used.[0, key, 0]is a get: return the stored value, or-1if the key is absent. A successful get makes the key most recently used; a miss does not change recency.
When inserting a new key into a full cache, evict the least recently used key. Updating an existing key does not evict another key. Return only the get results, in operation order. Each operation should take O(1) average time.
Function
lruCache(capacity: int, operations: int[][]) → int[]Examples
Example 1
capacity = 2operations = [[1,1,1],[1,2,2],[0,1,0],[1,3,3],[0,2,0],[1,4,4],[0,1,0],[0,3,0],[0,4,0]]return = [1,-1,-1,3,4]Getting key 1 refreshes it, so inserting key 3 evicts key 2. Inserting key 4 later evicts key 1. The get results are [1,-1,-1,3,4].
Example 2
capacity = 1operations = [[1,7,2],[1,7,9],[0,7,0]]return = [9]The second put replaces key 7 without adding an entry. Its get returns 9.
Constraints
1 <= capacity <= 3000.0 <= operations.length <= 200000.- Every operation has exactly three integers and starts with 0 or 1.
0 <= key <= 10000.0 <= value <= 100000.- For a get, the third integer is 0.