-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path432.zy445566.js
85 lines (77 loc) · 2.02 KB
/
432.zy445566.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
/**
* Initialize your data structure here.
*/
var AllOne = function() {
this.map = {};
this.valMap = {};
};
AllOne.prototype.changeNum = function(key,beforeNum,afterNum) {
if (beforeNum>0) {
if (this.valMap.hasOwnProperty(beforeNum)) {
this.valMap[beforeNum].splice(this.valMap[this.map[key]].indexOf(key),1)
}
}
if (afterNum>0) {
if (this.valMap.hasOwnProperty(afterNum)) {
this.valMap[afterNum].push(key);
} else {
this.valMap[afterNum]=[key];
}
}
}
/**
* Inserts a new key <Key> with value 1. Or increments an existing key by 1.
* @param {string} key
* @return {void}
*/
AllOne.prototype.inc = function(key) {
if (this.map.hasOwnProperty(key)) {
this.changeNum(key,this.map[key],this.map[key]+1)
this.map[key]++;
} else {
this.changeNum(key,0,1)
this.map[key] = 1;
}
};
/**
* Decrements an existing key by 1. If Key's value is 1, remove it from the data structure.
* @param {string} key
* @return {void}
*/
AllOne.prototype.dec = function(key) {
if (this.map.hasOwnProperty(key)) {
if (this.map[key]>1) {
this.changeNum(key,this.map[key],this.map[key]-1)
this.map[key]--;
} else {
this.changeNum(key,1,0)
delete this.map[key];
}
}
};
/**
* Returns one of the keys with maximal value.
* @return {string}
*/
AllOne.prototype.getMaxKey = function() {
let max = Math.max.apply(this,Object.values(this.map));
if (max==-Infinity) {return '';}
return this.valMap[max][0];
};
/**
* Returns one of the keys with Minimal value.
* @return {string}
*/
AllOne.prototype.getMinKey = function() {
let min = Math.min.apply(this,Object.values(this.map));
if (min==Infinity) {return '';}
return this.valMap[min][0];
};
/**
* Your AllOne object will be instantiated and called as such:
* var obj = Object.create(AllOne).createNew()
* obj.inc(key)
* obj.dec(key)
* var param_3 = obj.getMaxKey()
* var param_4 = obj.getMinKey()
*/