leetcode460. LFU緩存

請你爲 最不經常使用(LFU)緩存算法設計並實現數據結構。它應該支持以下操作:get 和 put。
get(key) - 如果鍵存在於緩存中,則獲取鍵的值(總是正數),否則返回 -1。
put(key, value) - 如果鍵已存在,則變更其值;如果鍵不存在,請插入鍵值對。當緩存達到其容量時,則應該在插入新項之前,使最不經常使用的項無效。在此問題中,當存在平局(即兩個或更多個鍵具有相同使用頻率)時,應該去除 最近 最少使用的鍵。
「項的使用次數」就是自插入該項以來對其調用 get 和 put 函數的次數之和。使用次數會在對應項被移除後置爲 0 。

進階:
你是否可以在 O(1) 時間複雜度內執行兩項操作?

示例:
LFUCache cache = new LFUCache( 2 /* capacity (緩存容量) */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // 返回 1
cache.put(3, 3); // 去除 key 2
cache.get(2); // 返回 -1 (未找到key 2)
cache.get(3); // 返回 3
cache.put(4, 4); // 去除 key 1
cache.get(1); // 返回 -1 (未找到 key 1)
cache.get(3); // 返回 3
cache.get(4); // 返回 4

答:
其實着道題的意思理解之後,就可以有思路了。
就是按照使用次數times排序,當times一致時,誰更晚操作就把誰放在後面。
本來計劃使用PriorityQueue的,但是隻有在刪除最小節點和插入時較快(也是O(lgn),不是O(1)),對於查詢很慢很慢。
第二反應是,使用TreeMap+鏈表的方式,TreeMap的key是times(應該是可行的)。
最後使用的是使用鏈表保存數據,保持鏈表有序(以times排序)。
光只有鏈表也不行,每次移動可能會要很久,所以加入lastMap,來進行快速移動(比如,我的node的times由3改成4,那我就移動到last(4)後面)。

代碼:

class LFUCache {
    Node head;
    int capacity;
    int sum = 0;
    //保存key和Node的關係
    Map<Integer, Node> ansMap;
    //保存times和Node關係(每次移動節點時候,知道移動到哪)
    Map<Integer, Node> lastMap;
    //當capacity爲1時,直接使用
    int key = 0;
    int value = -1;

    public LFUCache(int capacity) {
        this.capacity = capacity;
        if (capacity <= 1) {
            return;
        } else {
            ansMap = new HashMap<>(capacity);
            lastMap = new HashMap<>();
        }
    }

    public int get(int key) {
        if (capacity <= 0) {
            return -1;
        } else if (capacity == 1) {
            if (key == this.key) {
                return value;
            } else {
                return -1;
            }
        }
        Node ans = ansMap.get(key);
        if (ans != null) {
            //校驗是否在lastMap中
            checkLast(ans);
            //次數+1
            ans.times++;
            //移動node的位置
            moveNext(ans);
        } else {
            return -1;
        }
        return ans.value;
    }

    public void put(int key, int value) {
        if (capacity <= 0) {
            return;
        }
        if (capacity == 1) {
            this.key = key;
            this.value = value;
            return;
        }
        Node node = new Node();
        node.times = 1;
        node.key = key;
        node.value = value;
        if (head == null) {
            //head
            head = node;
            ansMap.put(key, node);
            lastMap.put(1, node);
            sum++;
        } else {
            Node ans = ansMap.get(key);
            if (ans != null) {
                //這時候的操作其實和get差不多,除了更新value值
                checkLast(ans);
                ans.times++;
                ans.value = value;
                moveNext(ans);
            } else {
                if (sum >= capacity) {
                    //我只保存capacity個數據,所以要移除head
                    ansMap.remove(head.key);
                    if (head.times != head.next.times) {
                        lastMap.remove(head.times);
                    }
                    head = head.next;
                }
                ansMap.put(key, node);
                if (head.times > 1) {
                    //我要插在首位了
                    node.next = head;
                    head.pre = node;
                    head = node;
                    lastMap.put(1, head);
                } else {
                    addNext(lastMap.get(1), node);
                }
                sum++;
            }
        }
    }

    //如果屬於lastMap,進行刪除
    private void checkLast(Node node) {
        if (lastMap.get(node.times) == node) {
            if (node.pre != null && node.pre.times == node.times) {
                lastMap.put(node.times, node.pre);
            } else {
                lastMap.remove(node.times);
            }
        }
    }

    //在nodeL後面插入node
    private void addNext(Node nodeL, Node node) {
        lastMap.put(node.times, node);
        if (nodeL.next == null) {
            nodeL.next = node;
            node.pre = nodeL;
            node.next = null;
            return;
        } else {
            nodeL.next.pre = node;
            node.next = nodeL.next;
            nodeL.next = node;
            node.pre = nodeL;
        }
    }

    private void moveNext(Node node) {
        Node next = node.next;
        if (next == null || node.times < next.times) {
            //無需移動,只要修改lastMap
            lastMap.put(node.times, node);
            return;
        }
        //移除node位置
        if (node == head) {
            head = node.next;
        } else {
            node.pre.next = node.next;
            node.next.pre = node.pre;
        }
        Node add = lastMap.get(node.times);
        if (add == null) {
            //都是k次,且不是last,恰好k+1次無數據,這時候插在last(k)後面
            addNext(lastMap.get(node.times - 1), node);
        } else {
            //插在add後面
            addNext(add, node);
        }
    }
}

class Node {
    int times;
    int key;
    int value;
    Node pre;
    Node next;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章