460. LFU Cache

Description

Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: get and put.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least frequently used item before inserting a new item. For the purpose of this problem, when there is a tie (i.e., two or more keys that have the same frequency), the least recently used key would be evicted.

Follow up:
Could you do both operations in O(1) time complexity?

Example:

LFUCache cache = new LFUCache( 2 /* capacity */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // returns 1
cache.put(3, 3);    // evicts key 2
cache.get(2);       // returns -1 (not found)
cache.get(3);       // returns 3.
cache.put(4, 4);    // evicts key 1.
cache.get(1);       // returns -1 (not found)
cache.get(3);       // returns 3
cache.get(4);       // returns 4

Solution

Two HashMap + DoublyLinkedList, O(1), S(capacity)

真的是难题啊,逻辑算很复杂的了,非常容易出bug。
思路基于LRU Cache,为了支持O(1) evict lease frequently used element,需要有一个freqMap<Integer, DLL>,还需要一个int minFreq,这样就够了。

一个比较tricky的地方是,minFreq的更新好像很麻烦。但实际上没有想象中的复杂,minFreq的更新是有迹可循的。

  • 当update node时,要判断node是否已经存在:
    • 如果node存在,node.freq++,如果node.oldFreq == minFreq,可能需要删除minFreqList,这时只需判断minFreqList在更新之后是否为空即可。
      • 不为空:minFreq保持不变
      • 为空:minFreq++即可,因为node.newFreq = node.oldFreq + 1,且它一定变成了新的minFreq
    • 如果node不存在,那么需要新建node,node.freq = 1。minFreq自然就变成1了,因为1是可能出现的最小freq!
  • 当evict node时,感觉minFreq的更新会变得复杂是不是,因为可能需要找到secondMinFreq对吗?但实际情况很简单,因为evict node一定是跟initialize node成对儿出现的(只有在达到capacity时),所以可以回归到上面的update node node为空的情况,minFreq一定会变成1。刺不刺激?

注意以下几个坑:

  • Node或List的创建或销毁,一定要跟Map的更新同步操作!否则会出现不一致。
  • 注意处理capacity == 0的情况
class LFUCache {

    class Node {
        Node prev, next;
        int key, val, freq;
        
        public Node(int k, int v) {
            key = k;
            val = v;
            freq = 1;   // initial freq is always 1
        }
    }
    
    class DLL {
        Node head, tail;
        int size;
        
        public DLL() {
            head = new Node(0, 0);
            tail = new Node(0, 0);
            head.next = tail;
            tail.prev = head;
        }
        // add node after head
        public void add(Node node) {
            head.next.prev = node;
            node.next = head.next;
            node.prev = head;
            head.next = node;
            ++size;
        }
        
        public void remove(Node node) {
            node.prev.next = node.next;
            node.next.prev = node.prev;
            --size;
        }
        
        public Node removeLast() {
            Node last = tail.prev;
            remove(last);
            return last;
        }
        
        public boolean isEmpty() {
            return size == 0;
        }
    }
    
    private int capacity;
    private int minFreq;
    private Map<Integer, Node> nodeMap;
    private Map<Integer, DLL> freqMap;
    
    public LFUCache(int capacity) {
        this.capacity = capacity;
        minFreq = 0;    // initial minFreq is 0 of course
        nodeMap = new HashMap<>();
        freqMap = new HashMap<>();
    }
    
    public int get(int key) {
        if (!nodeMap.containsKey(key)) {
            return -1;
        }
        Node node = nodeMap.get(key);
        update(node);
        return node.val;
    }
    
    public void put(int key, int val) {
        if (capacity < 1) { // if capacity is 0, we shouldn't make any change
            return;
        }
        
        if (nodeMap.containsKey(key)) {
            Node node = nodeMap.get(key);
            node.val = val;
            update(node);
        } else {
            if (nodeMap.size() == capacity) {
                evict();
            }
            
            Node node = new Node(key, val);
            nodeMap.put(key, node);
            minFreq = 1;    // update minFreq to 1! Because curr node is a new one
            DLL newList = freqMap.getOrDefault(node.freq, new DLL());
            freqMap.put(node.freq, newList);
            newList.add(node);
        }
    }
    // handle the tricky logic to move node from oldFreqList to newFreqList
    // don't forget to update minFreq if old freq list is the minFreq and empty
    private void update(Node node) {
        // deal with old freq list
        int oldFreq = node.freq;
        DLL oldList = freqMap.get(oldFreq);
        oldList.remove(node);
        if (oldList.isEmpty()) {    // evict empty freq list
            freqMap.remove(oldFreq);
            if (oldFreq == minFreq) {   // if the removed freq list is the min
                ++minFreq;  // just increase the minFreq because freqs are consequtive!
            }
        }
        // deal with new freq list
        ++node.freq;
        DLL newList = freqMap.getOrDefault(node.freq, new DLL());
        freqMap.put(node.freq, newList);
        newList.add(node);
    }
    // evict lease frequently used node
    private void evict() {
        Node last = freqMap.get(minFreq).removeLast();
        nodeMap.remove(last.key);
    }
}

/**
 * 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);
 */

Three HashMap + LinkedHashSet, O(1), S(capacity)

在Java中可以利用LinkedHashSet来当做DoublyLinkedList。

A LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class in used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were inserted.when cycling through LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.

LinkedHashSet内部应该是一个HashMap + DoublyLinkedList,其特性为:

  • 包含HashSet的一切特性:unique value,O(1) get, put
  • remains insertion order:用iterator遍历时会按照插入顺序从老到新。

下面的代码就是用LinkedHashSet作为DLL的实现,思路跟上面的方案是一致的。

class LFUCache {
    private int capacity;
    private int minFreq;
    private Map<Integer, Integer> valMap;
    private Map<Integer, Integer> freqMap;
    private Map<Integer, Set<Integer>> freqSetMap;
    
    public LFUCache(int capacity) {
        this.capacity = capacity;
        minFreq = 0;
        valMap = new HashMap<>();
        freqMap = new HashMap<>();
        freqSetMap = new HashMap<>();
    }
    
    public int get(int key) {
        if (!valMap.containsKey(key)) {
            return -1;
        }
        
        update(key);
        return valMap.get(key);
    }
    
    public void put(int key, int val) {
        if (capacity < 1) {
            return;
        }
        
        if (valMap.containsKey(key)) {
            valMap.put(key, val);
            update(key);
        } else {
            if (valMap.size() == capacity) {
                evict();
            }
            
            valMap.put(key, val);
            freqMap.put(key, 1);
            freqSetMap.put(1, freqSetMap.getOrDefault(1, new LinkedHashSet<>()));
            freqSetMap.get(1).add(key);
            minFreq = 1;
        }
    }
    
    private void update(int key) {
        int oldFreq = freqMap.get(key);
        int newFreq = oldFreq + 1;
        freqMap.put(key, newFreq);
        
        Set<Integer> oldFreqSet = freqSetMap.get(oldFreq);
        oldFreqSet.remove(key);
        if (oldFreqSet.isEmpty()) {
            freqSetMap.remove(oldFreq);
            if (oldFreq == minFreq) {
                ++minFreq;
            }
        }
        
        Set<Integer> newFreqSet = freqSetMap.getOrDefault(newFreq, new LinkedHashSet<>());
        freqSetMap.put(newFreq, newFreqSet);
        newFreqSet.add(key);
    }
    
    private void evict() {
        Set<Integer> minFreqSet = freqSetMap.get(minFreq);
        int lastKey = minFreqSet.iterator().next();
        minFreqSet.remove(lastKey);
        valMap.remove(lastKey);
        freqMap.remove(lastKey);
        
        if (minFreqSet.isEmpty()) {
            freqSetMap.remove(minFreq); // minFreq should be set to 1 later
        }
    }
}

/**
 * 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);
 */
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,569评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,499评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,271评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,087评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,474评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,670评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,911评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,636评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,397评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,607评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,093评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,418评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,074评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,092评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,865评论 0 196
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,726评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,627评论 2 270

推荐阅读更多精彩内容