Loading problem…
Implement LFUCache(capacity) with get(key) and put(key, value). A successful get returns the stored value; a missing key returns -1. Put inserts a new key or updates its value.
A new key starts with frequency 1. Each successful get and each put on an existing key increases its frequency by 1 and makes it the most recently used at its new frequency. A failed get changes nothing.
When inserting into a full cache, evict the key with the lowest frequency. Break frequency ties by evicting the least recently used key. Both methods must run in average O(1) time.
Tests provide named operations and values arrays. The constructor is first; constructor and put results are represented by null in the output.
Example 1:
Example 2:
Example 3: