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 CacheAll O`one Data Structure.

上一篇:eclipse在Ubuntu 13.04下的安装过程


下一篇:leetcode 460. LFU Cache