首页 > 系统服务 > 详细

LFU Cache

时间:2019-07-17 14:23:44      阅读:97      评论:0      收藏:0      [点我收藏+]

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

题解:https://www.cnblogs.com/Dylan-Java-NYC/p/6475008.html

去掉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.

 1 public class LFUCache {
 2     HashMap<Integer, Integer> keyVals;
 3     HashMap<Integer, Integer> keyCounts;
 4     HashMap<Integer, LinkedHashSet<Integer>> countKeySets;
 5     int capacity;
 6     int min;
 7 
 8     public LFUCache(int capacity) {
 9         this.capacity = capacity;
10         this.min = -1;
11         keyVals = new HashMap<>();
12         keyCounts = new HashMap<>();
13         countKeySets = new HashMap<>();
14         countKeySets.put(1, new LinkedHashSet<Integer>());
15     }
16 
17     public int get(int key) {
18         if (!keyVals.containsKey(key)) {
19             return -1;
20         }
21         int count = keyCounts.get(key);
22         keyCounts.put(key, count + 1);
23         countKeySets.get(count).remove(key);
24         if (count == min && countKeySets.get(count).size() == 0) {
25             min++;
26         }
27         if (!countKeySets.containsKey(count + 1)) {
28             countKeySets.put(count + 1, new LinkedHashSet<Integer>());
29         }
30         countKeySets.get(count + 1).add(key);
31         return keyVals.get(key);
32     }
33 
34     public void put(int key, int value) {
35         if (capacity <= 0) {
36             return;
37         }
38 
39         if (keyVals.containsKey(key)) {
40             keyVals.put(key, value);
41             get(key);
42             return;
43         }
44         if (keyVals.size() >= capacity) {
45             int leastFreq = countKeySets.get(min).iterator().next();
46             keyVals.remove(leastFreq);
47             keyCounts.remove(leastFreq);
48             countKeySets.get(min).remove(leastFreq);
49         }
50         keyVals.put(key, value);
51         keyCounts.put(key, 1);
52         countKeySets.get(1).add(key);
53         min = 1;
54     }
55 }

 

LFU Cache

原文:https://www.cnblogs.com/beiyeqingteng/p/11200281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!