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 ...
随机推荐
- PAT 天梯赛 L1-032. Left-pad 【字符串】
题目链接 https://www.patest.cn/contests/gplt/L1-032 思路 要分两种情况处理 ①字符串长度 <= 填充长度 就在字符串前面输出(填充长度 - 字符串长度 ...
- Ag-grid控件使用pine:left后,配合iview下拉框,会出现闪烁
Ag-grid控件使用pinned:left后,配合iview下拉框,会出现闪烁 引起原因:下拉图标的反转动画 目前解决方案: 添加一个全局样式: 禁用动画,其他地方也是如此, 影响控件有:gz-tr ...
- windows 服务安装报错
使用windows服务开发的定时任务,在win7上都运行良好,在windows server 2008上运行报错,报错信息如下 错误应用程序名称: GCWindowsService.exe,版本: 1 ...
- linux下pycharm的使用
百度搜索pycharm 然后打开pycharm的官网 然后在官网首页点击down 如果使用的是Linux系统,那么默认已经选择Linux版本 左边的down是全功能的IDE和WEB扩展,属于商业版 ...
- Spark及其生态系统简介总结
Spark拥有DAG执行引擎,支持在内存中对数据进行迭代计算 Spark不仅支持Scala编写应用程序,而且支持Java和Python等语言进行编写,特别是Scala是一种高效.可拓展的语言,能够用简 ...
- 使用Navicat连接oracle时出现unsupported server character set ZHS16GBK的解决之道
原文网址http://blog.mn886.net/chenjianhua/show/ba1dc6f835be403ea159b0a5e2685ff2/index.html ORA-12737:Ins ...
- Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Expiring 1 record(s) for topic_test_1219-2: 30010 ms has passed since batch creatio
代码如下 public static void producer1() throws ExecutionException, InterruptedException { Properties pro ...
- HDU 5183 Negative and Positive (NP) (hashmap+YY)
学到了以邻接表方式建立的hashmap 题意:给你一串数a和一个数k,都有正有负,问知否能找到一对数(i,j)(i<=j)保证a [i] - a [i+1] + a [i+2] - a [i+3 ...
- skynet中的各种锁
最近读skynet c语言部分的源码,发现有好多锁的使用和gcc提供的一些原子操作.看到这些东西,对于我这个newbee来说实在有些hold不住.但为了了解并进一步掌握,还是决定好好分析一下.不足之处 ...
- vc 自删除
// delself.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <windows.h>#include &l ...