460. LFU Cache Hard
1/**
2 * [460] LFU Cache
3 *
4 * Design and implement a data structure for a <a href="https://en.wikipedia.org/wiki/Least_frequently_used" target="_blank">Least Frequently Used (LFU)</a> cache.
5 * Implement the LFUCache class:
6 *
7 * LFUCache(int capacity) Initializes the object with the capacity of the data structure.
8 * int get(int key) Gets the value of the key if the key exists in the cache. Otherwise, returns -1.
9 * 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.
10 *
11 * 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.
12 * 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.
13 * The functions <code data-stringify-type="code">get and <code data-stringify-type="code">put must each run in O(1) average time complexity.
14 *
15 * Example 1:
16 *
17 * Input
18 * ["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"]
19 * [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]]
20 * Output
21 * [null, null, null, 1, null, -1, 3, null, -1, 3, 4]
22 * Explanation
23 * // cnt(x) = the use counter for key x
24 * // cache=[] will show the last used order for tiebreakers (leftmost element is most recent)
25 * LFUCache lfu = new LFUCache(2);
26 * lfu.put(1, 1); // cache=[1,_], cnt(1)=1
27 * lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
28 * lfu.get(1); // return 1
29 * // cache=[1,2], cnt(2)=1, cnt(1)=2
30 * lfu.put(3, 3); // 2 is the LFU key because cnt(2)=1 is the smallest, invalidate 2.
31 * // cache=[3,1], cnt(3)=1, cnt(1)=2
32 * lfu.get(2); // return -1 (not found)
33 * lfu.get(3); // return 3
34 * // cache=[3,1], cnt(3)=2, cnt(1)=2
35 * lfu.put(4, 4); // Both 1 and 3 have the same cnt, but 1 is LRU, invalidate 1.
36 * // cache=[4,3], cnt(4)=1, cnt(3)=2
37 * lfu.get(1); // return -1 (not found)
38 * lfu.get(3); // return 3
39 * // cache=[3,4], cnt(4)=1, cnt(3)=3
40 * lfu.get(4); // return 4
41 * // cache=[4,3], cnt(4)=2, cnt(3)=3
42 *
43 *
44 * Constraints:
45 *
46 * 0 <= capacity <= 10^4
47 * 0 <= key <= 10^5
48 * 0 <= value <= 10^9
49 * At most 2 * 10^5 calls will be made to get and put.
50 *
51 *
52 * <span style="display: none;"> </span>
53 */
54pub struct Solution {}
55
56// problem: https://leetcode.com/problems/lfu-cache/
57// discuss: https://leetcode.com/problems/lfu-cache/discuss/?currentPage=1&orderBy=most_votes&query=
58
59// submission codes start here
60
61struct LFUCache {
62 vec![]
63 }
64
65
66/**
67 * `&self` means the method takes an immutable reference.
68 * If you need a mutable reference, change it to `&mut self` instead.
69 */
70impl LFUCache {
71
72 fn new(capacity: i32) -> Self {
73
74 }
75
76 fn get(&self, key: i32) -> i32 {
77
78 }
79
80 fn put(&self, key: i32, value: i32) {
81
82 }
83}
84
85/**
86 * Your LFUCache object will be instantiated and called as such:
87 * let obj = LFUCache::new(capacity);
88 * let ret_1: i32 = obj.get(key);
89 * obj.put(key, value);
90 */
91
92// submission codes end
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_460() {
100 }
101}
102
Back
© 2025 bowen.ge All Rights Reserved.