|
| 1 | +460\. LFU Cache |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +Design and implement a data structure for a [Least Frequently Used (LFU)](https://en.wikipedia.org/wiki/Least_frequently_used) cache. |
| 6 | + |
| 7 | +Implement the `LFUCache` class: |
| 8 | + |
| 9 | +* `LFUCache(int capacity)` Initializes the object with the `capacity` of the data structure. |
| 10 | +* `int get(int key)` Gets the value of the `key` if the `key` exists in the cache. Otherwise, returns `-1`. |
| 11 | +* `void put(int key, int value)` Update the value of the `key` if present, or inserts the `key` if not already present. When the cache reaches its `capacity`, it should invalidate and remove the **least frequently used** key before inserting a new item. For this problem, when there is a **tie** (i.e., two or more keys with the same frequency), the **least recently used** `key` would be invalidated. |
| 12 | + |
| 13 | +To determine the least frequently used key, a **use counter** is maintained for each key in the cache. The key with the smallest **use counter** is the least frequently used key. |
| 14 | + |
| 15 | +When a key is first inserted into the cache, its **use counter** is set to `1` (due to the `put` operation). The **use counter** for a key in the cache is incremented either a `get` or `put` operation is called on it. |
| 16 | + |
| 17 | +The functions `get` and `put` must each run in `O(1)` average time complexity. |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +**Input** |
| 22 | + |
| 23 | + ["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"] |
| 24 | + [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] |
| 25 | + |
| 26 | +**Output:** [null, null, null, 1, null, -1, 3, null, -1, 3, 4] |
| 27 | + |
| 28 | +**Explanation:** |
| 29 | + |
| 30 | + // cnt(x) = the use counter for key x |
| 31 | + |
| 32 | + // cache=[] will show the last used order for tiebreakers (leftmost element is most recent) |
| 33 | + |
| 34 | + LFUCache lfu = new LFUCache(2); |
| 35 | + lfu.put(1, 1); // cache=[1,_], cnt(1)=1 |
| 36 | + lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 |
| 37 | + lfu.get(1); // return 1 |
| 38 | + // cache=[1,2], cnt(2)=1, cnt(1)=2 |
| 39 | + lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2. |
| 40 | + // cache=[3,1], cnt(3)=1, cnt(1)=2 |
| 41 | + lfu.get(2); // return -1 (not found) |
| 42 | + lfu.get(3); // return 3 |
| 43 | + // cache=[3,1], cnt(3)=2, cnt(1)=2 |
| 44 | + lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1. |
| 45 | + // cache=[4,3], cnt(4)=1, cnt(3)=2 |
| 46 | + lfu.get(1); // return -1 (not found) |
| 47 | + lfu.get(3); // return 3 |
| 48 | + // cache=[3,4], cnt(4)=1, cnt(3)=3 |
| 49 | + lfu.get(4); // return 4 |
| 50 | + // cache=[3,4], cnt(4)=2, cnt(3)=3 |
| 51 | + |
| 52 | +**Constraints:** |
| 53 | + |
| 54 | +* <code>0 <= capacity <= 10<sup>4</sup></code> |
| 55 | +* <code>0 <= key <= 10<sup>5</sup></code> |
| 56 | +* <code>0 <= value <= 10<sup>9</sup></code> |
| 57 | +* At most <code>2 * 10<sup>5</sup></code> calls will be made to `get` and `put`. |
0 commit comments