-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode--146-LRUcache.js
151 lines (133 loc) · 3.61 KB
/
leetcode--146-LRUcache.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
var Node = function (key, value) {
this.key = key;
this.val = value;
this.prev = this.next = null;
};
var DoublyLinkedList = function () {
this.head = new Node();
this.tail = new Node();
this.head.next = this.tail;
this.tail.prev = this.head;
};
DoublyLinkedList.prototype.insertHead = function (node) {
node.prev = this.head;
node.next = this.head.next;
this.head.next.prev = node;
this.head.next = node;
};
DoublyLinkedList.prototype.removeNode = function (node) {
// console.log("trying to remove node:", node.key)
let prev = node.prev;
let next = node.next;
prev.next = next;
next.prev = prev;
};
DoublyLinkedList.prototype.moveToHead = function (node) {
this.removeNode(node);
this.insertHead(node);
};
DoublyLinkedList.prototype.removeTail = function () {
let tail = this.tail.prev;
this.removeNode(tail);
return tail.key;
};
var LRUCache = function (capacity) {
this.capacity = capacity;
this.currentSize = 0;
this.hash = new Map();
this.dll = new DoublyLinkedList();
};
LRUCache.prototype.get = function (key) {
let node = this.hash.get(key);
if (!node) return -1;
this.dll.moveToHead(node);
return node.val;
};
LRUCache.prototype.put = function (key, value) {
let node = this.hash.get(key);
if (node == null) {
// new node
let newNode = new Node(key, value);
this.hash.set(key, newNode);
this.dll.insertHead(newNode);
this.currentSize++;
if (this.currentSize > this.capacity) {
let tailKey = this.dll.removeTail();
this.hash.delete(tailKey);
this.currentSize--;
}
} else {
// existed node, update its value and move to head;
node.val = value;
this.dll.moveToHead(node);
}
};
// class Node {
// constructor(key, value) {
// this.key = key;
// this.val = value;
// this.prev = null;
// this.next = null;
// }
// }
// class DoubleLinkedList {
// constructor() {
// this.head = new Node();
// this.tail = new Node();
// this.head.next = this.tail;
// this.tail.next = this.head;
// }
// insertHead(node) {
// node.prev = this.head;
// node.next = this.head.next;
// this.head.next.prev = node;
// }
// removeNode(node) {
// let prev = node.prev;
// let next = node.next;
// prev.next = next;
// next.prev = prev;
// }
// moveToHead(node) {
// this.removeNode(node);
// this.insertHead(node);
// }
// removeTail() {
// let tail = this.tail.prev;
// this.removeNode(tail);
// return tail.key;
// }
// }
// class LRUCache {
// constructor(capacity) {
// this.capacity = capacity;
// this.currentSize = 0;
// this.hash = new Map();
// this.dll = new DoubleLinkedList();
// }
// get(key) {
// let node = this.hash.get(key);
// if (node === null || node === undefined) return -1;
// this.dll.moveToHead(node);
// return node.val;
// }
// put(key, value) {
// let node = this.hash.get(key);
// if (node === null || node === undefined) {
// // new node
// let newNode = new Node(key, value);
// this.hash.set(key, newNode);
// this.dll.insertHead(newNode);
// this.currentSize++;
// if (this.currentSize > this.capacity) {
// let tailKey = this.dll.removeTail();
// this.hash.delete(tailKey);
// this.currentSize--;
// }
// } else {
// // existed node, update its value and move to head;
// node.val = value;
// this.dll.moveToHead(node);
// }
// }
// }