LeetCode-460. LFU 缓存
题目来源
460. 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
提示:
0 <= capacity <= 1040 <= key <= 1050 <= value <= 109- 最多调用 
2 * 105次get和put方法 
题解分析
解法一:HashMap+PriorityQueue
class LFUCache {
    class Node implements Comparable{
        int key, value;
        int freq;// 访问的频次
        int time;// 访问的时间
        Node(){}
        Node(int key, int value, int freq, int time){
            this.key = key;
            this.value = value;
            this.freq = freq;
            this.time = time;
        }
        public int compareTo(Node node){
            if(freq != node.freq){
                return freq - node.freq;
            }
            return time - node.time;
        }
    }
    int time;
    int capacity;
    PriorityQueue que;
    Map cache;// key-表示待插入的key,value-表示Node节点,存储了访问频次等相关信息
    public LFUCache(int capacity) {
        time = 0;
        this.capacity = capacity;
        que = new PriorityQueue<>();
        cache = new HashMap<>();
    }
    
    public int get(int key) {
        if(!cache.containsKey(key)){
            return -1;
        }
        time++;
        Node node = cache.get(key);
        node.freq++;
        node.time = time;
        que.remove(node);
        que.offer(node);
        return node.value;
    }
    
    public void put(int key, int value) {
        if(capacity == 0){
            return;
        }
        time++;
        if(cache.containsKey(key)){
            Node node = cache.get(key);
            node.freq++;
            node.time = time;
            node.value = value;
            que.remove(node);
            que.offer(node);
            return;
        }
        if(cache.size() == capacity){
            Node node = que.poll();
            cache.remove(node.key);
        }
        Node node = new Node(key, value, 1, time);
        cache.put(key, node);
        que.offer(node);
    }
}
/**
 * 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);
 */