forked from adamespii/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_067.js
136 lines (116 loc) Β· 2.65 KB
/
problem_067.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class LFUClass {
constructor(limit) {
this.min = 0;
this.limit = limit;
// Stored map value
this.nodeMap = new Map(); //{{key, node}, {key, node}}
this.freqMap = new Map(); //{{freq, DLLNode}, {freq, DLLNode}}
// Create doubly linked list
this.head = new DLLNode(0, 0);
this.tail = new DLLNode(0, 0);
this.head.next = this.tail;
this.tail.prev = this.head;
}
/**
* Sets key to value
* @param {*} key
* @param {*} value
*/
set(key, value) {
if (this.capacity === 0) return;
let node;
if (this.nodeMap.has(key)) {
node = this.nodeMap.get(key);
node.value = value;
let oldNode = this.freqMap.get(node.count);
oldNode.remove(node);
if (node.cnt === this.min && oldNode.size === 0) {
this.min++;
}
node.count++;
if (this.freqMap.get(node.count)) {
this.freqMap.get(node.count).add(node);
} else {
let newNode = new DLLNode();
newNode.add(node);
this.freqMap.set(node.count, newNode);
}
} else {
node = new DLLNode(key, value);
this.nodeMap.set(key, node);
if (this.size === this.capacity) {
let lastNode = this.freqMap.get(this.min);
this.nodeMap.delete(lastNode.removeLast().key);
this.size--;
}
this.size++;
this.min = 1;
if (this.freqMap.get(node.count)) {
this.freqMap.get(node.count).add(node);
} else {
let newNode = new DLLNode();
newNode.add(node);
this.freqMap.set(node.count, newNode);
}
}
}
/**
* Returns a value at key
* @param {*} key
* @return {*}
*/
get(key) {
const node = this.nodeMap.get(key);
if (!node) return -1;
let oldNode = this.freqMap.get(node.count);
oldNode.remove(node);
if (node.count === this.min && oldNode.size === 0) {
this.min++;
}
node.count++;
if (this.freqMap.get(node.count)) {
this.freqMap.get(node.count).add(node);
} else {
let newNode = new DLLNode();
newNode.add(node);
this.freqMap.set(node.count, newNode);
}
return node.value;
}
/**
* Inserts node after the head. Adds the node to most recently used
* @param {DLLNode} node
*/
add(node) {
node.prev = this.head;
node.next = this.head.next;
this.head.next.prev = node;
this.head.next = node;
this.size++;
}
/**
* Removes the given node from the cache
* @param {DLLNode} node
*/
remove(node) {
const { prev, next } = node;
prev.next = next;
next.prev = prev;
this.size--;
}
}
class DLLNode {
/**
* Creates Doubly Linked List
* @param {*} key - key stored within node
* @param {*} value - value stored
*/
constructor(key, value) {
this.key = key;
this.value = value;
this.count = 1;
// Connection between nodes
this.next = null;
this.prev = null;
}
}