LFU 缓存
难度:
标签:
题目描述
请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。
实现 LFUCache
类:
LFUCache(int capacity)
- 用数据结构的容量capacity
初始化对象int get(int key)
- 如果键key
存在于缓存中,则获取键的值,否则返回-1
。void put(int key, int value)
- 如果键key
已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量capacity
时,则应该在插入新项之前,移除最不经常使用的项。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最久未使用 的键。
为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。
当一个键首次插入到缓存中时,它的使用计数器被设置为 1
(由于 put 操作)。对缓存中的键执行 get
或 put
操作,使用计数器的值将会递增。
函数 get
和 put
必须以 O(1)
的平均时间复杂度运行。
示例:
输入: ["LFUCache", "put", "put", "get", "put", "get", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [3], [4, 4], [1], [3], [4]] 输出: [null, null, null, 1, null, -1, 3, null, -1, 3, 4] 解释: // cnt(x) = 键 x 的使用计数 // cache=[] 将显示最后一次使用的顺序(最左边的元素是最近的) LFUCache lfu = new LFUCache(2); lfu.put(1, 1); // cache=[1,_], cnt(1)=1 lfu.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1 lfu.get(1); // 返回 1 // cache=[1,2], cnt(2)=1, cnt(1)=2 lfu.put(3, 3); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小 // cache=[3,1], cnt(3)=1, cnt(1)=2 lfu.get(2); // 返回 -1(未找到) lfu.get(3); // 返回 3 // cache=[3,1], cnt(3)=2, cnt(1)=2 lfu.put(4, 4); // 去除键 1 ,1 和 3 的 cnt 相同,但 1 最久未使用 // cache=[4,3], cnt(4)=1, cnt(3)=2 lfu.get(1); // 返回 -1(未找到) lfu.get(3); // 返回 3 // cache=[3,4], cnt(4)=1, cnt(3)=3 lfu.get(4); // 返回 4 // cache=[3,4], cnt(4)=2, cnt(3)=3
提示:
1 <= capacity <= 104
0 <= key <= 105
0 <= value <= 109
- 最多调用
2 * 105
次get
和put
方法
代码结果
运行时间: 395 ms, 内存: 75.9 MB
/*
* LFU Cache implementation using Java Streams
*
* The implementation is similar to the standard Java approach but with the use of streams where applicable for operations like filtering and collecting keys.
*/
import java.util.*;
import java.util.stream.Collectors;
class LFUCache {
private int capacity;
private int minFreq;
private Map<Integer, Integer> keyToValue;
private Map<Integer, Integer> keyToFreq;
private Map<Integer, LinkedHashSet<Integer>> freqToKeys;
public LFUCache(int capacity) {
this.capacity = capacity;
this.minFreq = 0;
this.keyToValue = new HashMap<>();
this.keyToFreq = new HashMap<>();
this.freqToKeys = new HashMap<>();
}
public int get(int key) {
if (!keyToValue.containsKey(key)) return -1;
int freq = keyToFreq.get(key);
freqToKeys.get(freq).remove(key);
if (freqToKeys.get(freq).isEmpty()) {
freqToKeys.remove(freq);
if (freq == minFreq) minFreq++;
}
freq++;
keyToFreq.put(key, freq);
freqToKeys.computeIfAbsent(freq, k -> new LinkedHashSet<>()).add(key);
return keyToValue.get(key);
}
public void put(int key, int value) {
if (capacity == 0) return;
if (keyToValue.containsKey(key)) {
keyToValue.put(key, value);
get(key); // Update frequency
return;
}
if (keyToValue.size() >= capacity) {
int evict = freqToKeys.get(minFreq).stream().findFirst().orElse(-1);
freqToKeys.get(minFreq).remove(evict);
if (freqToKeys.get(minFreq).isEmpty()) freqToKeys.remove(minFreq);
keyToValue.remove(evict);
keyToFreq.remove(evict);
}
keyToValue.put(key, value);
keyToFreq.put(key, 1);
freqToKeys.computeIfAbsent(1, k -> new LinkedHashSet<>()).add(key);
minFreq = 1;
}
}
解释
方法:
本题解使用了两个哈希表和一个双向链表来实现 LFU 缓存。第一个哈希表 freq 以访问频率为键,值为一个有序字典,按照访问时间的先后顺序存储键。第二个哈希表 key_to_freq 以键为键,值为对应的访问频率。双向链表用于在插入和删除操作时维护键的访问时间先后顺序。当缓存容量已满,且需要插入新的键值对时,会删除访问频率最低的键中最早访问的那一个。同时维护一个 min_freq 变量,表示当前缓存中访问频率的最小值,以便在删除操作时快速定位到需要删除的键。
时间复杂度:
O(1)
空间复杂度:
O(capacity)
代码细节讲解
🦆
在`LFUCache`的`put`方法中,如果键已存在,更新值的过程中为什么需要将键从当前访问频率的字典中删除再重新插入到下一个访问频率的字典中?
▷🦆
当`get`或`put`操作导致某个访问频率的字典变为空时,除了更新`min_freq`,是否还需要执行其他清理或更新操作?
▷🦆
如何确保在`get`和`put`方法中,更新键的访问频率和重新插入字典的操作是原子性的,以防止并发环境下的数据不一致?
▷🦆
如果在`LFUCache`实例化时传入的`capacity`为0,是否有必要初始化`freq`和`key_to_freq`哈希表?
▷相关问题
LRU 缓存
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现
LRUCache
类:LRUCache(int capacity)
以 正整数 作为容量capacity
初始化 LRU 缓存int get(int key)
如果关键字key
存在于缓存中,则返回关键字的值,否则返回-1
。void put(int key, int value)
如果关键字key
已经存在,则变更其数据值value
;如果不存在,则向缓存中插入该组key-value
。如果插入操作导致关键字数量超过capacity
,则应该 逐出 最久未使用的关键字。
函数 get
和 put
必须以 O(1)
的平均时间复杂度运行。
示例:
输入 ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] 输出 [null, null, null, 1, null, -1, null, -1, 3, 4] 解释 LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // 缓存是 {1=1} lRUCache.put(2, 2); // 缓存是 {1=1, 2=2} lRUCache.get(1); // 返回 1 lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3} lRUCache.get(2); // 返回 -1 (未找到) lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3} lRUCache.get(1); // 返回 -1 (未找到) lRUCache.get(3); // 返回 3 lRUCache.get(4); // 返回 4
提示:
1 <= capacity <= 3000
0 <= key <= 10000
0 <= value <= 105
- 最多调用
2 * 105
次get
和put