forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnline Election.cpp
More file actions
33 lines (30 loc) · 834 Bytes
/
Online Election.cpp
File metadata and controls
33 lines (30 loc) · 834 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
26
27
28
29
30
31
32
33
class TopVotedCandidate {
public:
vector<int> leading;
vector<int> times;
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
this->times = times;
unordered_map<int,int> M;
int maxi=0,current_max=persons[0];
for(int i:persons){
M[i]++;
maxi = max(M[i],maxi);
//Update current max
if(M[i]==maxi){
current_max = i;
}
if(M[i]>=M[current_max]){
leading.push_back(i);
}
else{
leading.push_back(current_max);
}
}
}
int q(int t) {
auto it = upper_bound(times.begin(),times.end(),t);
it--;
int index = it - times.begin();
return leading[index];
}
};