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?
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 4Solution
class LFUCache { MapvalMap; 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 Cache 题目链接:https://leetcode.com/problems... 这个题要求O(1)的复杂度。首先要做到get(key)是O(1),能想到的数据结...
摘要:在静态的频率分布下,性能也落后于因为其不再为不在缓存中的数据维护任何频率数据。可以详见的准入淘汰策略是新增一个新的元素时,判断使用该元素替换一个旧元素,是否可以提升缓存命中率。 1. Introduction LFU的局限: LFU实现需要维护大而复杂的元数据(频次统计数据等) 大多数实际工作负载中,访问频率随着时间的推移而发生根本变化(这是外卖业务不适合朴素LFU的根本原因) 针...
摘要:在静态的频率分布下,性能也落后于因为其不再为不在缓存中的数据维护任何频率数据。可以详见的准入淘汰策略是新增一个新的元素时,判断使用该元素替换一个旧元素,是否可以提升缓存命中率。 1. Introduction LFU的局限: LFU实现需要维护大而复杂的元数据(频次统计数据等) 大多数实际工作负载中,访问频率随着时间的推移而发生根本变化(这是外卖业务不适合朴素LFU的根本原因) 针...
摘要:缓存算法我是,我会统计每一个缓存数据的使用频率,我会把使用最少的缓存替换出缓存区。浏览器就是使用了我作为缓存算法。在缓存系统中找出最少最近的对象是需要较高的时空成本。再来一次机会的缓存算法,是对的优化。直到新的缓存对象被放入。 缓存 什么是缓存? showImg(https://segmentfault.com/img/bVusZg); 存贮数据(使用频繁的数据)的临时地方,因为取原始...
阅读 2107·2023-04-25 21:11
阅读 2982·2021-09-30 09:47
阅读 2291·2021-09-24 09:48
阅读 4460·2021-08-23 09:43
阅读 913·2019-08-30 15:54
阅读 582·2019-08-28 18:01
阅读 1421·2019-08-27 10:55
阅读 602·2019-08-27 10:55