Linked Lists A list and a map together
312. LFU Cache
Design a cache that holds at most capacity keys, each with a value. When the cache is full and a new key
comes in, the cache makes room by evicting the key that has been used the fewest times. If several keys share
that smallest count, the one among them that was used least recently is evicted. Write a class LFUCache
with a constructor and two methods:
LFUCache(capacity)creates an empty cache that can holdcapacitykeys.get(key)returns the value stored forkey, or-1ifkeyis not in the cache.put(key, value)storesvalueforkey. Ifkeyis already in the cache, its old value is replaced. Otherwisekeyis added, and if the cache already holdscapacitykeys, one key is evicted first by the rule above.
A key is used each time put stores it and each time get finds it. Its use count is the number of
times it has been used since it last entered the cache, so a key that is added has a count of 1, and a key
that was evicted and comes back starts again at 1. get and put must each run in O(1) time on average.
Your class is tested by replaying a list of calls on a new LFUCache(capacity). ops[i] names the
i-th call, "get" or "put", and keys[i] is the key it is called with. The puts take their values from
values in order, so the first put stores values[0], the second stores values[1], and so on. The answer
is the list of every value that get returns, in the order of the calls.
In C# and Go the methods are Get and Put. Go creates the cache with Constructor(capacity), and Rust
with LFUCache::new(capacity). The names in ops are the same in every language.
Example 1
Input: capacity = 3
ops = ["put",
"put",
"put",
"get",
"get",
"put",
"get",
"get",
"put",
"get",
"get"]
keys = [4, 7, 1, 4, 7, 9, 1, 9, 3, 4, 7]
values = [40, 70, 10, 90, 30]
[40,70,-1,90,-1,70]Explanation: Keys 4 and 7 have been used twice and key 1 once, so put(9, 90) evicts key 1. After get(9), all three
keys have a count of 2. Key 4's last use is the oldest of the three, so put(3, 30) evicts key 4.
Example 2
Input: capacity = 2
ops = ["put",
"put",
"put",
"put",
"get",
"get",
"get"]
keys = [6, 2, 6, 5, 6, 2, 5]
values = [60, 20, 66, 50]
[66,-1,50]Explanation: put(6, 66) replaces the value of key 6 and is a use, so key 6 now has a count of 2. Nothing is evicted,
because no key was added. Key 2 has the smaller count, so put(5, 50) evicts it.
Example 3
Input: capacity = 1
ops = ["get",
"put",
"get",
"get",
"put",
"get",
"get"]
keys = [5, 0, 0, 0, 1000000000, 0, 1000000000]
values = [0, 1000000000]
[-1,0,0,-1,1000000000]Explanation: The stored value 0 is a real value, not a miss. Key 0 has a count of 3, but it is the only key, so
put(1000000000, 1000000000) must evict it to make room.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ capacity ≤ 1051 ≤ ops.length ≤ 2 × 105- Each
ops[i]is"get"or"put". keys.lengthequalsops.length.values.lengthequals the number of"put"calls inops.0 ≤ keys[i] ≤ 1090 ≤ values[i] ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.