• 460. LFU Cache


    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.

    Note that the number of times an item is used is the number of calls to the get and put functions for that item since it was inserted. This number is set to zero when the item is removed.

    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

    class LFUCache {
        Map<Integer, Integer> val;
        Map<Integer, Integer> count;
        Map<Integer, LinkedHashSet<Integer>> list;
        int min;
        int cap;
        public LFUCache(int capacity) {
            val = new HashMap();
            count = new HashMap();
            list = new HashMap();
            min = 0;
            cap = capacity;
        }
        
        public int get(int key) {
            if(!val.containsKey(key)) return -1;
            update(key);
            return val.get(key);
        }
        
        public void update(int key){
            int cnt = count.get(key);
            int newcnt = cnt + 1;
            count.put(key, newcnt);
            list.get(cnt).remove(key);
            if(list.get(cnt).size() == 0 && cnt == min) min++;
            
            if(!list.containsKey(newcnt)) list.put(newcnt, new LinkedHashSet());
            list.get(newcnt).add(key);
        }
        
        public void put(int key, int value) {
            if(cap <= 0) return;
            if(val.containsKey(key)){
                val.put(key, value);
                update(key);
                return;
            }
            
            
            if(val.values().size() >= cap)
                delete();
            
            val.put(key, value);
            count.put(key, 1);
            if(!list.containsKey(1)) list.put(1, new LinkedHashSet());
            list.get(1).add(key);
            min = 1;
        }
        
        public void delete(){
            int k = list.get(min).iterator().next();
            count.remove(k);
            val.remove(k);
            list.get(min).remove(k);
        }
    }
    
    /**
     * 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);
     */

    三个hashmap来实现

    val:只存放新加入的(key, value),

    count:存key,frequency

    list:存frequency,这个频率下的key的list,用LinkedHashSet()来存,因为LFU是说,存的时候如果满了就删除频率最小的,我们用min来表示,那如果min代表的频率对应的list有多个怎么办?删除LRU,这个用LinkedHashSet实现,因为它总是总头开始iterate

    要注意的是:

    put的key可能之前就存在,这样我们update就可以了。put完新key之后min自动变为1

    update是更新频率和min

    总结:

    To achieve this we need 3 maps and a variable min.

    3 maps are: val <key, value>, count <key, frequency>, list <frequency, List of key>, and min is current least frequency

    For get , we check if it exists, if so, we update its frequency and return

    For update, we update val and list maps cuz they related  to frequency. When updating, there's one thing should be paid attention, that is probably we need to update min, when current min doesn't have keys, we do min++

    For put, we check if the key already exists, if so we update the key. Then check if capacity reach to limit. If not, we put new key-val into val map, put key-1 into count map, and reset min to 1 cuz it's new. If we need to delete, we delete the first key related to min.

  • 相关阅读:
    C++ map的基本操作和用法
    堆排序汇总
    gdb调试多进程和多线程命令
    Linux内存分配机制
    svn 修改文件的可执行权限
    proc/sys/net/ipv4/下各项的意义
    linux read()和write
    ps命令参数
    /etc/passwd- 和/etc/shadow-文件
    openssh源码分析笔记
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13234531.html
Copyright © 2020-2023  润新知