460. LFU 缓存
请你为 最不经常使用(LFU)缓存算法设计并实现数据结构。
实现 LFUCache 类:
LFUCache(int capacity) - 用数据结构的容量 capacity 初始化对象
int get(int key) - 如果键存在于缓存中,则获取键的值,否则返回 -1。
void put(int key, int value) - 如果键已存在,则变更其值;如果键不存在,请插入键值对。当缓存达到其容量时,则应该在插入新项之前,使最不经常使用的项无效。在此问题中,当存在平局(即两个或更多个键具有相同使用频率)时,应该去除 最久未使用 的键。
注意「项的使用次数」就是自插入该项以来对其调用 get 和 put 函数的次数之和。使用次数会在对应项被移除后置为 0 。
为了确定最不常使用的键,可以为缓存中的每个键维护一个 使用计数器 。使用计数最小的键是最久未使用的键。
当一个键首次插入到缓存中时,它的使用计数器被设置为 1 (由于 put 操作)。对缓存中的键执行 get 或 put 操作,使用计数器的值将会递增。
示例:
输入:
["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 lFUCache = new LFUCache(2);
lFUCache.put(1, 1); // cache=[1,_], cnt(1)=1
lFUCache.put(2, 2); // cache=[2,1], cnt(2)=1, cnt(1)=1
lFUCache.get(1); // 返回 1
// cache=[1,2], cnt(2)=1, cnt(1)=2
lFUCache.put(3, 3); // 去除键 2 ,因为 cnt(2)=1 ,使用计数最小
// cache=[3,1], cnt(3)=1, cnt(1)=2
lFUCache.get(2); // 返回 -1(未找到)
lFUCache.get(3); // 返回 3
// cache=[3,1], cnt(3)=2, cnt(1)=2
lFUCache.put(4, 4); // 去除键 1 ,1 和 3 的 cnt 相同,但 1 最久未使用
// cache=[4,3], cnt(4)=1, cnt(3)=2
lFUCache.get(1); // 返回 -1(未找到)
lFUCache.get(3); // 返回 3
// cache=[3,4], cnt(4)=1, cnt(3)=3
lFUCache.get(4); // 返回 4
// cache=[3,4], cnt(4)=2, cnt(3)=3
运行结果
解题思路
主要是为了实现删除使用频率最低的页面,如果最低频率有多个,还需要删除最开始的那个
那么在LRU的参考下
需要存储key和val的关系---KV表
需要存储key和Freq的关系----KF表
需要存储Freq和对应的key列表-----FK表
(同时key列表又需要有序,所以使用LinkedHashSet)
确定好数据结构之后就是要注意三个表的更新同步,同时需要记录minFreq
java代码
class LFUCache {
// key 到 val 的映射,我们后文称为 KV 表
HashMap<Integer, Integer> keyToVal;
// key 到 freq 的映射,我们后文称为 KF 表
HashMap<Integer, Integer> keyToFreq;
// freq 到 key 列表的映射,我们后文称为 FK 表
HashMap<Integer, LinkedHashSet<Integer>> freqToKeys;
// 记录最小的频次
int minFreq;
// 记录 LFU 缓存的最大容量
int cap;
public LFUCache(int capacity) {
keyToFreq=new HashMap<>();
keyToVal=new HashMap<>();
freqToKeys=new HashMap<>();
this.cap=capacity;
minFreq=0;
}
public int get(int key) {
if(!keyToVal.containsKey(key)){
return -1;
}
increaseFreq(key);
return keyToVal.get(key);
}
public void put(int key, int value) {
if(this.cap <= 0) return;
//若存在,则更新值,freq加一
if(keyToVal.containsKey(key)){
keyToVal.put(key,value);
increaseFreq(key);
return;
}
if(this.cap <= keyToVal.size()){
//删除LFU的页面(最小频率)
removeMinFreqKey();
}
/* 插入 key 和 val,对应的 freq 为 1 */
// 插入 KV 表\KF 表\FK表
keyToVal.put(key,value);
keyToFreq.put(key,1);
freqToKeys.putIfAbsent(1,new LinkedHashSet<Integer>());
freqToKeys.get(1).add(key);
//加入一个新的,所以最小为1
this.minFreq=1;
}
public void increaseFreq(int key){
//更新KF表
int freq=keyToFreq.get(key);
keyToFreq.put(key,freq+1);
//从FK表的freq中删除
freqToKeys.get(freq).remove(key);
//加入freq+1
freqToKeys.putIfAbsent(freq+1,new LinkedHashSet<Integer>());
freqToKeys.get(freq+1).add(key);
//若该freq为空,则删除,同时若为最小freq,则minFreq加一
if(freqToKeys.get(freq).isEmpty()){
freqToKeys.remove(freq);
if(freq == this.minFreq){
this.minFreq++;
}
}
}
public void removeMinFreqKey(){
//获取到最小freq对应的list的第一个key,将其删除
LinkedHashSet<Integer> list=freqToKeys.get(minFreq);
int deleteKey=list.iterator().next();
list.remove(deleteKey);
//若该freq为空,则删除
if(list.isEmpty()){
freqToKeys.remove(minFreq);
}
//KV表和KF表删除
keyToVal.remove(deleteKey);
keyToFreq.remove(deleteKey);
}
}
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache obj = new LFUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
京公网安备 11010502036488号