1. 程式人生 > 其它 >LeetCode-460. LFU 快取

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 操作)。對快取中的鍵執行 getput 操作,使用計數器的值將會遞增。

函式 getput 必須以 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<= 104
  • 0 <= key <= 105
  • 0 <= value <= 109
  • 最多呼叫 2 * 105getput 方法

題解分析

解法一:HashMap+PriorityQueue

class LFUCache {
    class Node implements Comparable<Node>{
        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<Node> que;
    Map<Integer, Node> 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);
 */
Either Excellent or Rusty