-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLFU Cache.js
78 lines (75 loc) · 2.1 KB
/
LFU Cache.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @param {number} capacity
*/
var LFUCache = function(capacity) {
this.capacity = capacity;
this.cache = [];
};
/**
* @param {number} key
* @return {number}
*/
LFUCache.prototype.get = function(key) {
let val = -1;
if (!this.capacity) return val;
const existIndex = this.cache.findIndex(item => item.key === key);
if (existIndex > -1) {
const item = this.cache[existIndex];
val = item.value;
item.count++;
this.cache.splice(existIndex, 1);
this.cache.unshift(item);
}
return val;
};
/**
* @param {number} key
* @param {number} value
* @return {void}
*/
LFUCache.prototype.put = function(key, value) {
if (!this.capacity) return;
const existIndex = this.cache.findIndex(item => item.key === key);
if (existIndex > -1) {
// new item already exists,rewrite the value and increase count
const existItem = this.cache[existIndex];
existItem.value = value;
existItem.count++;
this.cache.splice(existIndex, 1);
this.cache.unshift(existItem);
} else {
// new item doesn't exist
if (this.cache.length === this.capacity) {
// reach the capacity, need to clear LFU
let lfuIndex = 0;
let leastCount = this.cache[lfuIndex].count;
for (let i = 1; i < this.cache.length; i++) {
const item = this.cache[i];
if (item.count <= leastCount) {
leastCount = item.count;
lfuIndex = i;
}
}
this.cache.splice(lfuIndex, 1);
// after clear LFU, push the new item
this.cache.unshift({
key,
value,
count: 1
});
} else {
// new item can be pushed
this.cache.unshift({
key,
value,
count: 1
});
}
}
};
/**
* Your LFUCache object will be instantiated and called as such:
* var obj = new LFUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/