lfu-cache(需要O(1),所以挺难的)
https://leetcode.com/problems/lfu-cache/
很难,看了下面的参考:
https://discuss.leetcode.com/topic/69137/java-o-1-accept-solution-using-hashmap-doublelinkedlist-and-linkedhashset
注意其中的思想就是如下所述:
Your idea is brilliant... Especially storing all keys with same counts in one node, if one of the keys in that node got hit once more,
it will be moved into a new node with (count+1) if the node exits or it will be wrapped into a newly created node with
(count+1).
All your operations are guaranteed O(1) no doubt. There is no way to complete it bug-free within half an hour. So in the real interview,
I might as well explain the idea and how we should implement all operations in each scenario,
instead of actually trying to complete whole program...
Anyway, thank you so much for your time and explanation.
并且注意,用到了LinkedHashSet的特性,就是虽然是Set,但是是按照顺序插入的方式来遍历的。
public class LFUCache {
private Node head = null;
private int cap = 0;
private HashMap<Integer, Integer> valueHash = null;
private HashMap<Integer, Node> nodeHash = null;
public LFUCache(int capacity) {
this.cap = capacity;
valueHash = new HashMap<Integer, Integer>();
nodeHash = new HashMap<Integer, Node>();
}
public int get(int key) {
if (valueHash.containsKey(key)) {
increaseCount(key);
return valueHash.get(key);
}
return -1;
}
public void set(int key, int value) {
if ( cap == 0 ) return;
if (valueHash.containsKey(key)) {
valueHash.put(key, value);
Node node = nodeHash.get(key);
node.keys.remove(key);
node.keys.add(key);
} else {
if (valueHash.size() < cap) {
valueHash.put(key, value);
} else {
removeOld();
valueHash.put(key, value);
}
addToHead(key);
}
increaseCount(key);
}
private void addToHead(int key) {
if (head == null) {
head = new Node(0);
head.keys.add(key);
} else if (head.count > 0) {
Node node = new Node(0);
node.keys.add(key);
node.next = head;
head.prev = node;
head = node;
} else {
head.keys.add(key);
}
nodeHash.put(key, head);
}
private void increaseCount(int key) {
Node node = nodeHash.get(key);
node.keys.remove(key);
if (node.next == null) {
node.next = new Node(node.count+1);
node.next.prev = node;
node.next.keys.add(key);
} else if (node.next.count == node.count+1) {
node.next.keys.add(key);
} else {
Node tmp = new Node(node.count+1);
tmp.keys.add(key);
tmp.prev = node;
tmp.next = node.next;
node.next.prev = tmp;
node.next = tmp;
}
nodeHash.put(key, node.next);
if (node.keys.size() == 0) remove(node);
}
private void removeOld() {
if (head == null) return;
int old = 0;
for (int n: head.keys) {
old = n;
break;
}
head.keys.remove(old);
if (head.keys.size() == 0) remove(head);
nodeHash.remove(old);
valueHash.remove(old);
}
private void remove(Node node) {
if (node.prev == null) {
head = node.next;
} else {
node.prev.next = node.next;
}
if (node.next != null) {
node.next.prev = node.prev;
}
}
class Node {
public int count = 0;
public LinkedHashSet<Integer> keys = null;
public Node prev = null, next = null;
public Node(int count) {
this.count = count;
keys = new LinkedHashSet<Integer>();
prev = next = null;
}
}
}
/**
* Your LFUCache object will be instantiated and called as such:
* LFUCache obj = new LFUCache(capacity);
* int param_1 = obj.get(key);
* obj.set(key,value);
*/
lfu-cache(需要O(1),所以挺难的)的更多相关文章
- [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 ...
- LFU Cache
2018-11-06 20:06:04 LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”. ...
- LeetCode LFU Cache
原题链接在这里:https://leetcode.com/problems/lfu-cache/?tab=Description 题目: Design and implement a data str ...
- POJ 1275 Cashier Employment 挺难的差分约束题
http://poj.org/problem?id=1275 题目大意: 一商店二十四小时营业,但每个时间段需求的雇员数不同(已知,设为R[i]),现有n个人申请这份工作,其可以从固定时间t连续工作八 ...
- [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 ...
- 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
随机推荐
- 详解Python中的相对导入和绝对导入
Python 相对导入与绝对导入,这两个概念是相对于包内导入而言的.包内导入即是包内的模块导入包内部的模块. Python import 的搜索路径 在当前目录下搜索该模块 在环境变量 PYTHONP ...
- 请求报文&响应报文
转自黑马程序员视频教程
- 反转单词顺序 VS 左旋转字符串
题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标垫符号和普通字母一样处理.例如输入字符串“I am a student.”,则输出“student. a am I ...
- loj2275 「JXOI2017」颜色
枚举右端点,然后看左端点合法情况. 先预处理每个颜色 \(i\) 的最大出现位置 \(max_i\) 和最小出现位置 \(min_i\).对于枚举右端点在一个位置 \(i\),凡是 \(max_k & ...
- 写iOS SDK注意事项
转载http://www.devtang.com/blog/2015/01/31/write-sdk-tips/
- 用Python表达对Android的想法
组员:喻航,张子东 视频:点我 #DISCARD ANDROID TODAY! import turtle import turtle as gui #setting turtle.screensiz ...
- oracle中的dual表
dual表是和Oracle数据字典一起创建的.它实际上只包含dummy这一个column,并且只有一条记录,这条记录的值是X. X dual表的owner是SYS,但所有用户都可以访问它.Althou ...
- HDU-2448 Mining Station on the Sea
先根据不同的起点跑最短路,记录距离,从而建立二分图求最小匹配. 一开始我求最短路的时候我把港口直接加到图中,然后发现进了港口就不能出来了,所以连接港口的边就要从双向边改成单向边…………这也搞得我n和m ...
- BZOJ 4551 [Tjoi2016&Heoi2016]树 ——并查集
树剖显然可以做. 然而有一种更神奇的方法,并查集+时光倒流. 每个节点指向它上面最近的标记节点,标记节点指向自己,然后删除标记,就可以用并查集查询了. #include <map> #in ...
- 本博客由CSDN迁移而来,以前的博文可能显示不正常
如题,原博客地址 http://blog.csdn.net/vicjiao 或点击右侧友链