forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1157.cpp
More file actions
25 lines (24 loc) · 695 Bytes
/
1157.cpp
File metadata and controls
25 lines (24 loc) · 695 Bytes
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
class MajorityChecker
{
public:
MajorityChecker(vector<int>& arr)
{
data = arr;
for(int i = 0; i < arr.size(); ++i) indexs[data[i]].push_back(i);
}
int query(int left, int right, int threshold)
{
for(int i = 0; i < 7; ++i)
{
int x = left + rand() % (right - left + 1);
int num = data[x];
auto l = lower_bound(indexs[num].begin(), indexs[num].end(), left);
auto r = upper_bound(indexs[num].begin(), indexs[num].end(), right);
if (r - l >= threshold) return num;
}
return -1;
}
private:
vector<int> data;
unordered_map<int, vector<int>> indexs;
};