码迷,mamicode.com
首页 > 系统相关 > 详细

460. LFU Cache

时间:2020-07-04 13:49:45      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:recent   amp   void   就删除   code   reac   ted   shm   read   

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

460. LFU Cache

标签:recent   amp   void   就删除   code   reac   ted   shm   read   

原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13234531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!