All O`one Data Structure LeetCode solution in JavaScript

1
var AllOne = function () {
2
this.map = new Map();
3
this.pre = "";
4
};
5

6
/**
7
* @param {set} map
8
* @param {function} handler
9
* @return {map}
10
*/
11
AllOne.prototype.sort = function (map, handler) {
12
return new Map([...map].sort(handler));
13
};
14

15
/**
16
* @param {string} key
17
* @return {void}
18
*/
19
AllOne.prototype.inc = function (key) {
20
this.map.set(key, this.map.get(key) + 1 || 1);
21
this.pre = "inc";
22
};
23

24
/**
25
* @param {string} key
26
* @return {void}
27
*/
28
AllOne.prototype.dec = function (key) {
29
this.map.get(key) == 1
30
? this.map.delete(key)
31
: this.map.set(key, this.map.get(key) - 1);
32
this.pre = "dec";
33
};
34

35
/**
36
* @return {string}
37
*/
38
AllOne.prototype.getMaxKey = function () {
39
if (this.pre != "max") {
40
this.map = this.sort(this.map, (a, b) => b[1] - a[1]);
41
}
42
this.pre = "max";
43
return this.map.keys().next().value || "";
44
};
45

46
/**
47
* @return {string}
48
*/
49
AllOne.prototype.getMinKey = function () {
50
if (this.pre != "min") {
51
this.map = this.sort(this.map, (a, b) => a[1] - b[1]);
52
}
53
this.pre = "min";
54
return this.map.keys().next().value || "";
55
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0