forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1157.js
42 lines (40 loc) · 1.11 KB
/
1157.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
*/
var MajorityChecker = function(arr) {
this.data = arr;
this.indexs = {};
for (var i = 0; i < arr.length; ++i) {
if (!this.indexs[arr[i]]) this.indexs[arr[i]] = [];
this.indexs[arr[i]].push(i);
}
};
/**
* @param {number} left
* @param {number} right
* @param {number} threshold
* @return {number}
*/
MajorityChecker.prototype.query = function(left, right, threshold) {
for (var i = 0; i < 7; ++i) {
var num = this.data[Math.round(Math.random()*(right - left) + left)];
var l = 0, r = this.indexs[num].length - 1;
while (l < r) {
var mid = l + r >> 1;
if (this.indexs[num][mid] >= left) {
r = mid;
} else l = mid + 1;
}
var t1 = l, t2 = 0;
l = 0, r = this.indexs[num].length - 1;
while (l < r) {
var mid = l + r + 1 >> 1;
if (this.indexs[num][mid] <= right) {
l = mid;
} else r = mid - 1;
}
t2 = l + 1;
if (t2 - t1 >= threshold) return num;
}
return -1;
};