Design a Number Container System LeetCode solution in JavaScript

1
var NumberContainers = function () {
2
this.obj = {};
3
this.global = {};
4
};
5

6
NumberContainers.prototype.change = function (index, number) {
7
if (this.global[index]) {
8
for (var key in this.obj) {
9
let ind = this.obj[key].indexOf(index);
10
if (ind != -1) {
11
this.obj[key].splice(ind, 1);
12
if (this.obj[number]) this.obj[number].push(index);
13
else this.obj[number] = [index];
14

15
this.global[index] = 1;
16
return;
17
}
18
}
19
}
20

21
if (this.obj[number]) this.obj[number].push(index);
22
else this.obj[number] = [index];
23
this.global[index] = 1;
24
};
25

26
NumberContainers.prototype.find = function (number) {
27
if (this.obj[number]) {
28
if (this.obj[number].length == 0) return -1;
29
else return Math.min(...this.obj[number]);
30
}
31
return -1;
32
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0