oneby7

Linked Lists A list and a map together

312. LFU Cache

Hard

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 hold capacity keys.
  • get(key) returns the value stored for key, or -1 if key is not in the cache.
  • put(key, value) stores value for key. If key is already in the cache, its old value is replaced. Otherwise key is added, and if the cache already holds capacity keys, 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]

Output: [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]

Output: [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]

Output: [-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.

capacity = 2 ops = ["put", "put", "put", "put", "put", "get", "get", "get"] keys = [1, 2, 2, 1, 3, 1, 2, 3] values = [10, 20, 21, 11, 30]

Constraints

  • 1 ≤ capacity ≤ 105
  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is "get" or "put".
  • keys.length equals ops.length.
  • values.length equals the number of "put" calls in ops.
  • 0 ≤ keys[i] ≤ 109
  • 0 ≤ values[i] ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.