LeetCode 460. LFU Cache
来源:互联网 发布:免费销售软件 编辑:程序博客网 时间:2024/06/10 14:19
题目:
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.
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 1cache.put(3, 3); // evicts key 2cache.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 3cache.get(4); // returns 4
思路:
变量
- key——
键
- value——
值
- num——
key被使用的次数,包括get和put(对已有key的put是更新,也要num++)
- key——
容器
HashMap<Integer,int[]> valueHashMap
——存储<key,int[value,num]>
TreeMap<Integer,LinkedHashSet<Integer>> numTreeMap
——存储num,list<key>
numTreeMap为了确定容量不足时该删除谁,其中TreeMap能在O(1)时间复杂度保证最小frequency,LinkedHashSet能在O(1)时间复杂度remove开头key,remove任意key,add。
valueHashMap中存储的num是一份冗余信息,相当于索引,保证plusToTreeMap时,可以在O(1)的时间复杂度锁定位置。
代码:
public class LFUCache { private TreeMap<Integer,LinkedHashSet<Integer>> numTreeMap; // k:num,v:timeLinkedHashSet private HashMap<Integer,int[]> valueHashMap; // k:key,v:int[value,num] private int capacity; public LFUCache(int capacity) { this.capacity = capacity; numTreeMap = new TreeMap<>(); valueHashMap = new HashMap<>(); } //这些调试用的打印语句,一定要记得注释掉,毕竟打印时间复杂度是O(n)... public int get(int key) { // System.out.println(numTreeMap.toString()); // System.out.println("~~~~~~~~~~~~~~~~~~~~get ("+key+")"); if(!valueHashMap.containsKey(key)){ // System.out.println("value: "+ -1); return -1; } int v = valueHashMap.get(key)[0]; plusToTreeMap(key); // System.out.println("value: "+ v); return v; } public void put(int key, int value) { // System.out.println(numTreeMap.toString()); // System.out.println("~~~~~~~~~~~~~~~~~~~~put ("+key+","+value+")"); if(capacity==0){ return; } if(valueHashMap.containsKey(key)){ plusToTreeMap(key); valueHashMap.put(key,new int[]{value,valueHashMap.get(key)[1]}); }else{ if(valueHashMap.size()==capacity){ // System.out.println("remove!"); removeFromCache(); } valueHashMap.put(key,new int[]{value,1}); addToTreeMap(key); } } //新的key被put后,将valueHashMap中的num设成1,并在numTreeMap.get(1)中放入该key private void addToTreeMap(int key){ LinkedHashSet<Integer> timeLinkedList; if(!numTreeMap.containsKey(1)){ timeLinkedList = new LinkedHashSet<>(); numTreeMap.put(1,timeLinkedList); }else{ timeLinkedList = numTreeMap.get(1); } timeLinkedList.add(key); } //已有key被get或put后,要修改两个地方,一个是valueHashMap中的num++,一个是提升该key在numTreeMap中的位置。 private void plusToTreeMap(int key){ LinkedHashSet<Integer> timeLinkedList; int num = valueHashMap.get(key)[1]++; timeLinkedList = numTreeMap.get(num); timeLinkedList.remove(key); if(timeLinkedList.size()==0){ numTreeMap.remove(num); } if(!numTreeMap.containsKey(num+1)){ timeLinkedList = new LinkedHashSet<>(); numTreeMap.put(num+1,timeLinkedList); }else{ timeLinkedList = numTreeMap.get(num+1); } timeLinkedList.add(key); } // freshTreeMap函数的作用是将key提到当前次数的最后,开始时我以为put操作不影响frequently只影响recently,才写的这个,对本题来说没有作用。实际cache设计中我觉得使用该函数也许更合理 // private void freshTreeMap(int key){ // LinkedHashSet<Integer> timeLinkedList; // int num = valueHashMap.get(key)[1]; // timeLinkedList = numTreeMap.get(num); // timeLinkedList.remove(key); // timeLinkedList.add(key); // } //容器满了,又put了新的key,就需要调用removeFromCache,清理出一个空间。思路是先在numTreeMap找到最小frequently对应的时间队列timeLinkedList,再找到该队列的第一个元素,将它删除。如果此时timeLinkedList空了,要在numTreeMap中把该frequently的对应的timeLinkedList删除,保证以后numTreeMap.firstEntry().getValue()找到的timeLinkedList一定非空。 private void removeFromCache(){ LinkedHashSet<Integer> timeLinkedList = numTreeMap.firstEntry().getValue(); int k = timeLinkedList.iterator().next(); timeLinkedList.remove(k); valueHashMap.remove(k); if(timeLinkedList.size()==0){ numTreeMap.remove(numTreeMap.firstEntry().getKey()); } }}/** * 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); */
- LeetCode 460. LFU Cache
- LeetCode 460. LFU Cache
- [LeetCode]460. LFU Cache
- Leetcode 460. LFU Cache
- Leetcode 460. LFU Cache
- leetcode 460. LFU Cache
- LeetCode 460. LFU Cache
- leetcode 146. LRU Cache 460. LFU Cache
- [Leetcode] 460. LFU Cache 解题报告
- [LeetCode] LFU Cache
- 460. LFU Cache
- 460. LFU Cache
- 460. LFU Cache
- 20170604-leetcode-460-LFU Cache
- LFU Cache
- LFU Cache
- 460. LFU Cache O(1)算法 C++
- [Leetcode-460]LFU Cache 最近不常用页面交换
- python初学七 list
- Leetcode122 - Best Time to Buy and Sell Stock II (dp)
- 代码对比工具winMerge
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- Hadoop学习笔记-002-CentOS_6.5_64_克隆系统后更改IP及主机名
- LeetCode 460. LFU Cache
- 再探完全背包 HDOJ 2069 Coin Change
- 乒乓球
- JNI全局引用与JFrame.dispose()方法
- java的getClass()函数
- 1004. Counting Leaves (30)
- Hadoop学习笔记-003-CentOS_6.5_64_设置ssh免密码登录
- 解决PHP Warning: strftime(): It is not safe to rely on the system's timezone set
- pybrain的神经网络文档部分翻译