-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_052.js
100 lines (87 loc) Β· 1.97 KB
/
problem_052.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
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;
// Connection between nodes
this.next = null;
this.prev = null;
}
}
class LRUCache {
/**
* Initializes LRUCache
* @param {number} limit - cache size of n
*/
constructor(limit) {
this.limit = limit;
// Stored map value
this.map = new Map();
// 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;
}
/**
* Returns a value at key
* @param {*} key
* @param {*} value
*/
set(key, value) {
if(this.map.has(key)) {
let addedNode = this.map.get(key);
addedNode.value = value;
// Update current list
this.remove(addedNode);
this.add(addedNode);
} else {
// Add new node to running list
let addedNode = new DLLNode(key, value);
this.map.set(key, addedNode);
this.add(addedNode);
// Check if new node goes over limit
if(this.map.size > this.limit) {
let lastNode = this.tail.prev;
this.remove(lastNode);
this.map.delete(lastNode.key);
}
}
}
/**
* Returns a value at key
* @param {*} key
* @return {*}
*/
get(key) {
if (!this.map.has(key)) return null;
// Retrieve/update recently used
let node = this.map.get(key);
this.remove(node);
this.add(node);
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;
}
/**
* Removes the given node from the cache
* @param {DLLNode} node
*/
remove(node) {
const { prev, next } = node;
prev.next = next;
next.prev = prev;
}
}