LeetCode LFU Cache
原题链接在这里:https://leetcode.com/problems/lfu-cache/
题目:
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the following operations: getand 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
题解:
去掉least frequently used element, 就需要一个min来maintain到目前最不被利用的元素的利用次数.
用三个map, 一个是维护正常key value pair的HashMap<Integer, Integer> keyVals.
第二个是维护每个key的使用次数.
第三个是维护每个count下对应的key set.
当put第一个元素时, min=1, 对应更新keyVals, keyCounts 和 countKeySets.
get时, key的count要加一, 对应调整keyCounts 和 countKeySets. 若这个key的count恰巧是最少使用次数的最后一个值,那么最少使用次数min++.
在达到capacity后在加新key时利用min来找到least frequently used element, 并对应调整keyVals, keyCounts 和 countKeySets.
Note: corner case capacity <= 0.
countToKeys need LinkedHashSet, because when there is even, evict the oldest one.
Time Complexity: get, O(1). put, O(1).
Space: O(n).
AC Java:
public class LFUCache {
HashMap<Integer, Integer> keyVals;
HashMap<Integer, Integer> keyCounts;
HashMap<Integer, LinkedHashSet<Integer>> countKeySets;
int capacity;
int min;
public LFUCache(int capacity) {
this.capacity = capacity;
this.min = -1;
keyVals = new HashMap<Integer, Integer>();
keyCounts = new HashMap<Integer, Integer>();
countKeySets = new HashMap<Integer, LinkedHashSet<Integer>>();
countKeySets.put(1, new LinkedHashSet<Integer>());
}
public int get(int key) {
if(!keyVals.containsKey(key)){
return -1;
}
int count = keyCounts.get(key);
keyCounts.put(key, count+1);
countKeySets.get(count).remove(key);
if(count == min && countKeySets.get(count).size() == 0){
min++;
}
if(!countKeySets.containsKey(count+1)){
countKeySets.put(count+1, new LinkedHashSet<Integer>());
}
countKeySets.get(count+1).add(key);
return keyVals.get(key);
}
public void put(int key, int value) {
if(capacity <= 0){
return;
}
if(keyVals.containsKey(key)){
keyVals.put(key, value);
get(key);
return;
}
if(keyVals.size() >= capacity){
int leastFreq = countKeySets.get(min).iterator().next();
keyVals.remove(leastFreq);
keyCounts.remove(leastFreq);
countKeySets.get(min).remove(leastFreq);
}
keyVals.put(key, value);
keyCounts.put(key, 1);
countKeySets.get(1).add(key);
min = 1;
}
}
/**
* 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);
*/
类似LRU Cache, All O`one Data Structure.
LeetCode LFU Cache的更多相关文章
- [LeetCode] LFU Cache 最近最不常用页面置换缓存器
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- [LeetCode] 460. LFU Cache 最近最不常用页面置换缓存器
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LFU Cache
2018-11-06 20:06:04 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”. ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
- leetcode 460. LFU Cache
hash:存储的key.value.freq freq:存储的freq.key,也就是说出现1次的所有key在一起,用list连接 class LFUCache { public: LFUCache( ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
随机推荐
- $Android自定义控件风格的方法
EditText在获取焦点后默认的边框都是黄色的,这可能和我在开发的应用的主题颜色不匹配,那怎么办呢?——用自定义的控件风格,比如说我想让EditText在获取焦点时候边框变成蓝色的,而失去焦点后边框 ...
- Linux用户和用户组管理 用户管理相关命令
用户添加命令 useradd 注意: 新添加的用户如果不设定密码是不能够登录系统的 命令格式: [root@localhost ~]#useradd [选项] 用户名 选项说明: 选项 选项说明 -u ...
- Hadoop单机搭建
单机Hadoop搭建 1.下载hadoop-2.7.3.tar.gz http://www.apache.org/dyn/closer.cgi/hadoop/common/hadoop-2.7.3/h ...
- 20145230《Java程序设计》第4周学习总结
20145230<Java程序设计>第4周学习总结 教材学习内容总结 继承共同行为 本周学习的首先是Java语言中的继承与多态.何为我们的继承呢?在我们面向对象中,子类继承父类,避免重复的 ...
- MySQL备份账号权限
grant select,show view,lock tables,trigger on confluence.* to 'DBbackup'@'127.0.0.1' identified by ' ...
- INSPIRED启示录 读书笔记 - 第20章 基本产品
消减功能还是延长工期 不要再试图定义最终产品,转而定义只满足基本要求的产品,简称基本产品 1.产品经理与设计师合作设计产品的高保真原型,这个原型只具备实现商业目标的最基本功能要求,以及良好的用户体验和 ...
- .NET自带泛型委托方法Func、Action和Predicate
Func.Action和Predicate是.NET自带的3个泛型委托方法,三个方法的区别其实并不大,要强行给混着用也是可以的,但是我们是有追求的人,把道理讲清楚总是好的. 一.Func是有返回值的方 ...
- Linux VPS实用简单安全配置
今天,和大家一起来分享VPS最基本的安全配置. 第一.修改SSH端口 VPS默认的SSH端口是22,那些扫描穷举密码的,也势必从22开始,所以,修改22为一个其他的数字,是非常有必要的. 好了,SSH ...
- vi编辑器快捷键使用详解
文本编辑器是所有计算机系统中最常用的一种工具.UNIX下的编辑器有ex,sed和vi等,其中,使用最为广泛的是vi,而vi命令繁多,论坛里好像这方面的总结不多,以下稍做总结,以资共享!渴望更正和补充! ...
- DataX-HDFS(读写)
DataX操作HDFS 读取HDFS 1 快速介绍 HdfsReader提供了读取分布式文件系统数据存储的能力.在底层实现上,HdfsReader获取分布式文件系统上文件的数据,并转换为DataX传输 ...