-
Notifications
You must be signed in to change notification settings - Fork 279
/
146 LRU Cache.js
206 lines (165 loc) · 3.86 KB
/
146 LRU 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
class Node {
constructor(key, val) {
this.key = key;
this.val = val;
this.next = null;
this.prev = null;
}
}
/**
* @constructor
*/
var LRUCache = function(capacity) {
this.list = null;
this.map = new Map();
this.head = null;
this.tail = null;
this.capacity = capacity;
this.curSize = 0;
};
/**
* @param {number} key
* @returns {number}
*/
LRUCache.prototype.get = function(key) {
let node = this.map.get(key);
if (!node) {
return -1;
}
if (node === this.head) {
return node.val;
}
// remove node from list
if (node === this.tail) {
this.tail.prev.next = null;
this.tail = this.tail.prev;
} else {
node.prev.next = node.next;
node.next.prev = node.prev;
}
// insert node to head
node.next = this.head;
this.head.prev = node;
this.head = node;
return node.val;
};
/**
* @param {number} key
* @param {number} value
* @returns {void}
*/
LRUCache.prototype.set = function(key, value) {
let newNode = new Node(key, value);
if (this.curSize === 0) {
this.tail = newNode;
} else {
newNode.next = this.head;
this.head.prev = newNode;
}
this.head = newNode;
// this.curSize++;
// update
if (this.map.get(key)) {
let oldNode = this.map.get(key);
// remove node
if (oldNode === this.tail) {
this.tail = this.tail.prev;
this.tail.next = null;
} else {
oldNode.prev.next = oldNode.next;
oldNode.next.prev = oldNode.prev;
}
} else {
this.curSize++
if (this.curSize > this.capacity) {
//delete tail
this.map.delete(this.tail.key);
this.tail = this.tail.prev;
this.tail.next = null;
this.curSize--;
}
}
this.map.set(key, newNode);
};
// Second Implementation
function DoublyLinkListNode(key, value) {
this.key = key;
this.value = value;
this.prev = this.next = null;
}
/**
* @constructor
*/
var LRUCache = function(capacity) {
this.head = this.tail = null;
this.maxCapacity = capacity;
this.currSize = 0;
this.hash = {};
};
/**
* @param {number} key
* @returns {number}
*/
LRUCache.prototype.get = function(key) {
if(!this.hash[key]) {
return -1;
}
this.moveToHead(key);
return this.hash[key].value;
};
/**
* @param {number} key
* @param {number} value
* @returns {void}
*/
LRUCache.prototype.set = function(key, value) {
if(this.maxCapacity <= 0) {
return;
}
if(!this.hash[key]) {
if(this.currSize === this.maxCapacity) {
this.removeLast();
this.currSize--;
}
this.hash[key] = new DoublyLinkListNode(key, value);
this.currSize++;
}
this.hash[key].value = value;
this.moveToHead(key);
};
LRUCache.prototype.removeLast = function() {
if(this.tail === null) {
return;
}
delete this.hash[this.tail.key];
var newTail = this.tail.prev;
if(newTail === null) {
this.head = this.tail = null;
return;
}
this.tail.prev = null;
newTail.next = null;
this.tail = newTail;
}
LRUCache.prototype.moveToHead = function(key) {
var newHead = this.hash[key];
if(this.head === null && this.tail === null) {
this.head = this.tail = newHead;
}
if(newHead === this.head) {
return;
}
if(newHead === this.tail) {
this.tail = newHead.prev;
}
if(newHead.prev) {
newHead.prev.next = newHead.next;
}
if(newHead.next) {
newHead.next.prev = newHead.prev;
}
newHead.prev = null;
newHead.next = this.head;
this.head.prev = newHead;
this.head = newHead;
}