-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesignANumberContainerSystem.js
55 lines (55 loc) · 1.33 KB
/
designANumberContainerSystem.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
var NumberContainers = function () {
this.m = new Map();
this.d = new Map();
};
NumberContainers.prototype.change = function (i, n) {
if (this.m.has(i) && this.m.get(i) == n) return;
this.m.set(i, n);
if (!this.d.has(n)) this.d.set(n, new MinHeap());
this.d.get(n).push(i);
};
NumberContainers.prototype.find = function (n) {
if (!this.d.has(n)) return -1;
let h = this.d.get(n);
while (h.size() && this.m.get(h.peek()) !== n) h.pop();
return h.size() ? h.peek() : -1;
};
function MinHeap() {
this.a = [];
}
MinHeap.prototype.push = function (x) {
this.a.push(x);
let i = this.a.length - 1;
while (i > 0) {
let p = (i - 1) >> 1;
if (this.a[p] <= this.a[i]) break;
[this.a[p], this.a[i]] = [this.a[i], this.a[p]];
i = p;
}
};
MinHeap.prototype.pop = function () {
let r = this.a[0],
l = this.a.pop();
if (this.a.length) {
this.a[0] = l;
let i = 0,
n = this.a.length;
while (true) {
let l = i * 2 + 1,
r = i * 2 + 2,
m = i;
if (l < n && this.a[l] < this.a[m]) m = l;
if (r < n && this.a[r] < this.a[m]) m = r;
if (m === i) break;
[this.a[i], this.a[m]] = [this.a[m], this.a[i]];
i = m;
}
}
return r;
};
MinHeap.prototype.peek = function () {
return this.a[0];
};
MinHeap.prototype.size = function () {
return this.a.length;
};