Instructions
Write a class LRUCache that stores key-value pairs but holds at most capacity keys. When it is full and a new key must go in, it removes the key that was used least recently.
The class has these methods:
LRUCache(capacity)creates an empty cache that holds at mostcapacitykeys.get(key)returns the value stored forkey, or-1if the key is not in the cache.put(key, value)storesvalueforkey. It returns nothing. If the key is already in the cache, its value is replaced. If the key is new and the cache already holdscapacitykeys, first remove the least recently used key, then add the new one.
What counts as a use
- A
getthat finds the key is a use of that key. - Every
putis a use of its key, whether the key was new or already there. - A
getthat does not find the key (returns-1) changes nothing.
The goal is O(1) time for each call to get and put.
How the input is shown
The test gives two lists of the same length. operations[i] is the name of a call and arguments[i] is the list of values for that call. The first call is always "LRUCache" with the capacity. For example, operations = ["LRUCache", "put", "get"] with arguments = [[2], [5, 50], [5]] means: create a cache of capacity 2, call put(5, 50), then call get(5).
The expected output is a list with one entry per call: the value the call returns, or null for the constructor and for put, which return nothing.
Hint: a hash map finds a key in O(1). To know which key is the oldest, keep the keys in a doubly linked list ordered by last use: move a key to the front when it is used, and remove from the back. (In JavaScript a Map remembers insertion order; in Python an OrderedDict does too.)
Examples
Example 1
- Input:
- ["LRUCache","put","put","get","put","get","get","get"], [[2],[4,40],[7,70],[4],[9,90],[7],[4],[9]]
- Output:
- [null,null,null,40,null,-1,40,90]
The cache holds 2 keys. get(4) makes key 4 the most recently used, so key 7 is now the least recently used. put(9, 90) needs room and removes key 7. After that, get(7) is -1 while keys 4 and 9 are still there.
Example 2
- Input:
- ["LRUCache","put","put","put","put","get","get"], [[2],[1,10],[2,20],[1,15],[3,30],[2],[1]]
- Output:
- [null,null,null,null,null,-1,15]
put(1, 15) replaces the value of key 1 and counts as a use, so key 2 becomes the least recently used. put(3, 30) then removes key 2. get(1) returns the new value 15.
Example 3
- Input:
- ["LRUCache","get","put","get","put","get","get"], [[1],[6],[6,0],[6],[8,5],[6],[8]]
- Output:
- [null,-1,null,0,null,-1,5]
With capacity 1 the cache holds one key. get(6) on the empty cache is -1. A stored value of 0 is a real value, not "missing". put(8, 5) removes key 6 to make room.
Constraints
1 <= capacity <= 40 <= key <= 90 <= value <= 99- At most
30calls togetandputin total, after the constructor.
3 examples run when you press Run. Submit checks every hidden test too.
- Input:
- ["LRUCache","put","put","get","put","get","get","get"], [[2],[4,40],[7,70],[4],[9,90],[7],[4],[9]]
- Expected:
- [null,null,null,40,null,-1,40,90]
- Input:
- ["LRUCache","put","put","put","put","get","get"], [[2],[1,10],[2,20],[1,15],[3,30],[2],[1]]
- Expected:
- [null,null,null,null,null,-1,15]
- Input:
- ["LRUCache","get","put","get","put","get","get"], [[1],[6],[6,0],[6],[8,5],[6],[8]]
- Expected:
- [null,-1,null,0,null,-1,5]
Run checks your code against the 3 examples. Submit checks it against all 73 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.