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: 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
题解:
去掉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 ...
随机推荐
- list列表、tuple元组、range常用方法总结
list 列表(数组),是可迭代对象,列表是可变的所以列表的方法都是在列表本身更改的.里面看可以放各种数据类型的数据,可存储大量数据 连接列表可以使用 + 或 extend() a = [1, 3, ...
- Jquery.ScrollLoading图片延迟加载技术
关于分屏加载图片,像天猫.京东等电商图片较多页面很长,就采用了延迟加载技术. 目前很流行的做法就是滚动动态加载,显示屏幕之外的图片默认是不加载的, 随着页面的滚动,显示区域图片才被动态加载. 原理其实 ...
- $Android去除系统默认的标题栏和全屏的三种方法
在做应用的时候,很多时候是不需要系统自带的标题栏的,而是自己去实现标题栏,这就要去掉系统的标题栏,下面总结了三种方法.全屏也是一样的道理,也总结了实现的三种方法. (一)去除标题栏 1.方法1 在Ac ...
- [转]u盘读不出来怎么办大汇总
今天遇到的问题 http://www.upantool.com/jiaocheng/xiufu/2016/9958.html u盘读不出来怎么办大汇总 2016-12-14 21:42 来源: 本站整 ...
- no xxx find in java.library.path
JAVA系统运行时候load native lib时候会遇到下面错误,如 java.lang.UnsatisfiedLinkError: no JSTAF in java.library.path这可 ...
- 使用 Apache Spark 让 MySQL 查询速度提升 10 倍以上
转: https://coyee.com/article/11012-how-apache-spark-makes-your-slow-mysql-queries-10x-faster-or-more ...
- 【[NOI2011]智能车比赛】(建图+spfa+坑爹精度)
过了这题我就想说一声艹,跟这个题死磕了将近6个小时,终于是把这个题死磕出来了.首先看到这个题的第一反应,和当初做过的一个房间最短路比较相似,然后考虑像那个题那样建边,然后跑最短路.(具体建边方法请参考 ...
- nohup后台运行jar与关闭
nohup 用途:LINUX命令用法,不挂断地运行命令. 语法:nohup Command [ Arg ... ] [ & ] 描述:nohup 命令运行由 Command 参数和任何相关 ...
- hive学习7(条件函数case)
case函数 语法: CASE WHEN a THEN b [WHEN c THEN d]* [ELSE e] END 说明:如果a为TRUE,则返回b:如果c为TRUE,则返回d:否则返回e 实例 ...
- Python DB API 连接数据库
Python DB API Mysql,Oracle,SqlServer 不关闭,会浪费资源.