资讯专栏INFORMATION COLUMN

[LeetCode] 460. LFU Cache

yacheng / 3055人阅读

Problem

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
class LFUCache {
    Map valMap;
    Map freqMap;
    Map> kSetMap;
    int size;
    int min;
    
    public LFUCache(int capacity) {
        min = 0;
        size = capacity;
        valMap = new HashMap<>();
        freqMap = new HashMap<>();
        kSetMap = new HashMap<>();
        kSetMap.put(1, new LinkedHashSet<>());
    }
    
    public int get(int key) {
        if (!valMap.containsKey(key)) return -1;
        
        //get frequency, then update freqMap, kSetMap, min
        int frequency = freqMap.get(key);
        freqMap.put(key, frequency+1);
        kSetMap.get(frequency).remove(key);
        if (!kSetMap.containsKey(frequency+1)) {
            kSetMap.put(frequency+1, new LinkedHashSet<>());
        }
        kSetMap.get(frequency+1).add(key);
        if (min == frequency && kSetMap.get(frequency).size() == 0) {
            min++;
        }
        
        return valMap.get(key);
        
    }
    
    public void put(int key, int value) {
        if (size <= 0) return;
        //when key is existed, just update valMap value, 
        //and call get(key) to update freqmap, kSetMap and min
        if (valMap.containsKey(key)) {
            valMap.put(key, value);
            get(key);
            return;
        }
        
        //when reached capacity, remove min in all 3 maps
        if (valMap.size() == size) {
            int minKey = kSetMap.get(min).iterator().next();
            valMap.remove(minKey);
            freqMap.remove(minKey);
            kSetMap.get(min).remove(minKey);
        }
        
        //add the fresh k-v pair to all 3 maps
        valMap.put(key, value);
        freqMap.put(key, 1);
        kSetMap.get(1).add(key);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77218.html

相关文章

  • LRU & LFU Cache

    摘要:首先要做到是,能想到的数据结构只有两三种,一个是,一个是,是,还有一个,是。不太可能,因为长度要而且不可变,题目也没说长度固定。可以做到和都是。因为还有函数,要可以,所以还需要一个数据结构来记录顺序,自然想到。 LRU Cache 题目链接:https://leetcode.com/problems... 这个题要求O(1)的复杂度。首先要做到get(key)是O(1),能想到的数据结...

    wenshi11019 评论0 收藏0
  • 论文《TinyLFU: A Highly Ecient Cache Admission Polic

    摘要:在静态的频率分布下,性能也落后于因为其不再为不在缓存中的数据维护任何频率数据。可以详见的准入淘汰策略是新增一个新的元素时,判断使用该元素替换一个旧元素,是否可以提升缓存命中率。 1. Introduction LFU的局限: LFU实现需要维护大而复杂的元数据(频次统计数据等) 大多数实际工作负载中,访问频率随着时间的推移而发生根本变化(这是外卖业务不适合朴素LFU的根本原因) 针...

    高璐 评论0 收藏0
  • 论文《TinyLFU: A Highly Ecient Cache Admission Polic

    摘要:在静态的频率分布下,性能也落后于因为其不再为不在缓存中的数据维护任何频率数据。可以详见的准入淘汰策略是新增一个新的元素时,判断使用该元素替换一个旧元素,是否可以提升缓存命中率。 1. Introduction LFU的局限: LFU实现需要维护大而复杂的元数据(频次统计数据等) 大多数实际工作负载中,访问频率随着时间的推移而发生根本变化(这是外卖业务不适合朴素LFU的根本原因) 针...

    RobinQu 评论0 收藏0
  • LFU

    摘要:如果每一个频率放在一个里面,每个也有头尾两个指针,指向相邻的。实际上相邻的可以由的第一可以由的最后一个唯一确认。也就是说,在的设计基础上。也就是说频率为的点,指向的下一个是频率为的点移除和一样。里存在的点,加到尾部的后一个。 只个代码由LRU改进得到。如果每一个频率放在一个LRU里面,每个LRU也有头尾两个指针,指向相邻的LRU。实际上相邻的LRU可以由frequency = t+1的...

    whidy 评论0 收藏0
  • 笔记|缓存

    摘要:缓存算法我是,我会统计每一个缓存数据的使用频率,我会把使用最少的缓存替换出缓存区。浏览器就是使用了我作为缓存算法。在缓存系统中找出最少最近的对象是需要较高的时空成本。再来一次机会的缓存算法,是对的优化。直到新的缓存对象被放入。 缓存 什么是缓存? showImg(https://segmentfault.com/img/bVusZg); 存贮数据(使用频繁的数据)的临时地方,因为取原始...

    elliott_hu 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<