-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path146-lru-cache.js
More file actions
87 lines (73 loc) · 1.99 KB
/
146-lru-cache.js
File metadata and controls
87 lines (73 loc) · 1.99 KB
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
class LRUCache {
constructor(capacity) {
this.capacity = capacity;
// Doubly linked list: maintains an ordered list and we can remove an element
// anywhere in the list in O(1) time and add to end of list in O(1) time
this.head = {};
this.tail = {};
this.head.next = this.tail; // head points to the lru element
this.tail.prev = this.head;
// type: key -> { key, value, prev, next }
this.cache = new Map();
}
addAtTail(key, value) {
const prevLastNode = this.tail.prev;
const newNode = {
key,
value,
};
// make old last node and
// new last node point to each other
newNode.prev = prevLastNode;
prevLastNode.next = newNode;
// make new last node the tail
newNode.next = this.tail;
this.tail.prev = newNode;
// add new tail node to cache
this.cache.set(key, newNode);
}
delete(key) {
const node = this.cache.get(key);
// update all the references
node.prev.next = node.next;
node.next.prev = node.prev;
// delete from the cache
this.cache.delete(key);
}
deleteFromHead() {
const headNodeKey = this.head.next.key;
this.delete(headNodeKey);
}
put(key, value) {
// corner case: already in the cache
if (this.cache.has(key)) {
// move the node to tail
this.delete(key);
this.addAtTail(key, value);
return;
}
// check if we have space in the cache
const hasSpace = this.cache.size < this.capacity;
if (hasSpace) {
// If so, just insert
this.addAtTail(key, value);
return;
}
// otherwise, evict lru then add the new
this.deleteFromHead();
this.addAtTail(key, value);
}
get(key) {
if (!this.cache.has(key)) return -1;
const { value } = this.cache.get(key);
this.delete(key);
this.addAtTail(key, value);
return value;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* var obj = new LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/