-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
42 lines (38 loc) · 983 Bytes
/
index.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
/**
* @param {number[]} arr
*/
let MajorityChecker = function(arr) {
this.data = arr;
this.total = {};
for (let ele of arr) {
this.total[ele] = this.total[ele] ? this.total[ele] + 1 : 1;
}
};
/**
* @param {number} left
* @param {number} right
* @param {number} threshold
* @return {number}
*/
MajorityChecker.prototype.query = function(left, right, threshold) {
const total = Object.assign({}, this.total);
for (let i = 0; i < left; i++) {
total[this.data[i]]--;
}
for (let i = right + 1; i < this.data.length; i++) {
total[this.data[i]]--;
}
for (let k in total) {
if (total[k] >= threshold) return k;
}
return -1;
};
/**
* Your MajorityChecker object will be instantiated and called as such:
* var obj = new MajorityChecker(arr)
* var param_1 = obj.query(left,right,threshold)
*/
const mc = new MajorityChecker([1,1,2,2,1,1]);
console.log(mc.query(0, 5, 4));
console.log(mc.query(0, 3, 3));
console.log(mc.query(2, 3, 2));