-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem52.js
120 lines (105 loc) · 3 KB
/
Problem52.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
// Problem 52
//
// This problem was asked by Google.
//
// Implement an LRU (Least Recently Used) cache.
// It should be able to be initialized with a cache size n, and contain the following methods:
//
// set(key, value): sets key to value. If there are already n items in the cache and we are adding a new item, then it should also remove the least recently used item.
// get(key): gets the value at key. If no such key exists, return null.
//
// Each operation should run in O(1) time.
//
// https://leetcode.com/problems/lru-cache/description/
//
// Both operations take O(1) Time complexity and O(N) Space complexity
// N is the number of key value pairs
class Node {
/**
* Initializes the doubly linked node with a key and value
* @param {*} key The key stored in node
* @param {*} value The value stored in value
*/
constructor(key, value) {
this.key = key;
this.value = value;
this.next = null;
this.prev = null;
}
}
class LRUCache {
/**
* Initializes LRUCache with capacity size
* @param {number} capacity
*/
constructor(capacity) {
this.capacity = capacity;
// map maps key to a node
this.map = new Map();
// nodes after the head are most recently used
this.head = new Node(0, 0);
this.tail = new Node(0, 0);
this.head.next = this.tail;
this.tail.prev = this.head;
}
/**
* Sets key to value in cache.
* If there are already n items in the cache and we are adding a new item, then it should also remove the least recently used item.
* @param {*} key
* @param {*} value
*/
set(key, value) {
if (this.map.has(key)) {
const node = this.map.get(key);
node.value = value;
// add to most recently used
this.removeNode(node);
this.addNode(node);
} else {
const newNode = new Node(key, value);
this.map.set(key, newNode);
this.addNode(newNode);
// over capacity, so remove the least recently used which is previous of tail
if (this.map.size > this.capacity) {
const lastItemNode = this.tail.prev;
this.removeNode(lastItemNode);
this.map.delete(lastItemNode.key);
}
}
}
/**
* Gets the value at key in cache. If no such key exists, return null.
* @param {*} key
* @return {*}
*/
get(key) {
if (!this.map.has(key)) return null;
// move key value node to most recently used
const node = this.map.get(key);
this.removeNode(node);
this.addNode(node);
return node.value;
}
/**
* Inserts node after the head. Makes the node the most recently used
* @param {Node} node
*/
addNode(node) {
// insertions after the head
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 {Node} node
*/
// eslint-disable-next-line class-methods-use-this
removeNode(node) {
const { prev, next } = node;
prev.next = next;
next.prev = prev;
}
}
export default LRUCache;