Skip to content

Commit 98d1de0

Browse files
author
luzhipeng
committed
1 parent adbbff0 commit 98d1de0

File tree

5 files changed

+95
-102
lines changed

5 files changed

+95
-102
lines changed

Diff for: README.en.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,15 @@ The data structures mainly includes:
197197
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
198198
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
199199
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
200-
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕
201-
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕
200+
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
201+
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
202202
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
203203
- [0146.lru-cache](./problems/146.lru-cache.md)
204204
- [0239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
205205
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
206206
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
207+
- [0460.lfu-cache](./problems/460.lfu-cache.md) 🆕
208+
207209

208210

209211

Diff for: README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,18 @@ leetcode 题解,记录自己的 leetcode 解题之路。
213213
- [1031.maximum-sum-of-two-non-overlapping-subarrays](./problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md) 🆕
214214

215215
#### 困难难度
216+
216217
- [0023.merge-k-sorted-lists](./problems/23.merge-k-sorted-lists.md)
217218
- [0032.longest-valid-parentheses](./problems/32.longest-valid-parentheses.md) 🆕
218219
- [0042.trapping-rain-water](./problems/42.trapping-rain-water.md)
219-
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md) 🆕
220-
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md) 🆕
220+
- [0124.binary-tree-maximum-path-sum](./problems/124.binary-tree-maximum-path-sum.md)
221+
- [0128.longest-consecutive-sequence](./problems/128.longest-consecutive-sequence.md)
221222
- [0145.binary-tree-postorder-traversal](./problems/145.binary-tree-postorder-traversal.md)
222223
- [0146.lru-cache](./problems/146.lru-cache.md)
223224
- [0239.sliding-window-maximum](./problems/239.sliding-window-maximum.md)
224225
- [0295.find-median-from-data-stream](./problems/295.find-median-from-data-stream.md) 🆕
225226
- [0301.remove-invalid-parentheses](./problems/301.remove-invalid-parentheses.md)
227+
- [0460.lfu-cache](./problems/460.lfu-cache.md) 🆕
226228

227229
### 数据结构与算法的总结
228230

Diff for: daily/answers/460.lfu-cache.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* @lc app=leetcode id=460 lang=javascript
3+
*
4+
* [460] LFU Cache
5+
*/
6+
/**
7+
* @param {number} capacity
8+
*/
9+
var LFUCache = function(capacity) {
10+
this.capacity = capacity;
11+
this.size = 0;
12+
this.cache = {};
13+
this.timestamp = 0;
14+
};
15+
16+
/**
17+
* @param {number} key
18+
* @return {number}
19+
*/
20+
LFUCache.prototype.get = function(key) {
21+
const hit = this.cache[key];
22+
23+
if (hit === void 0) {
24+
return -1;
25+
}
26+
hit.count += 1;
27+
hit.timestamp = this.timestamp++;
28+
29+
return hit.value;
30+
};
31+
32+
// 时间复杂度O(n) n其实就是capacity
33+
LFUCache.prototype.evicted = function() {
34+
// evicted lfu
35+
let leastCountKey = null;
36+
let min = Number.MAX_VALUE;
37+
38+
for (const k in this.cache) {
39+
const item = this.cache[k];
40+
if (item.count < min) {
41+
leastCountKey = k;
42+
min = item.count;
43+
} else if (
44+
item.count === min &&
45+
item.timestamp < this.cache[leastCountKey].timestamp
46+
) {
47+
leastCountKey = k;
48+
min = item.count;
49+
}
50+
}
51+
52+
delete this.cache[leastCountKey];
53+
this.size--;
54+
};
55+
56+
/**
57+
* @param {number} key
58+
* @param {number} value
59+
* @return {void}
60+
*/
61+
LFUCache.prototype.put = function(key, value) {
62+
if (this.capacity === 0) return;
63+
const hit = this.cache[key];
64+
65+
if (hit === void 0) {
66+
if (this.capacity === this.size) {
67+
this.evicted();
68+
}
69+
this.size++;
70+
return (this.cache[key] = {
71+
value,
72+
timestamp: this.timestamp++,
73+
count: 1
74+
});
75+
}
76+
77+
this.cache[key].value = value;
78+
this.cache[key].timestamp = this.timestamp++;
79+
return (this.cache[key].count += 1);
80+
};
81+
82+
/**
83+
* Your LFUCache object will be instantiated and called as such:
84+
* var obj = new LFUCache(capacity)
85+
* var param_1 = obj.get(key)
86+
* obj.put(key,value)
87+
*/

Diff for: todo/candidates/151.reverse-words-in-a-string.js

-17
This file was deleted.

Diff for: todo/candidates/460.lfu-cache.js

-81
This file was deleted.

0 commit comments

Comments
 (0)