-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path911.cpp
More file actions
48 lines (45 loc) · 1.14 KB
/
911.cpp
File metadata and controls
48 lines (45 loc) · 1.14 KB
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
class Node{
public:
int person, time;
Node(int p, int t){
person = p;
time = t;
}
};
class TopVotedCandidate {
public:
vector<Node> votes;
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
votes.clear();
vector<int> hashed(5002,0);
int curr = -1, vote = 0;
for(int i=0; i<persons.size(); i++){
int p = persons[i], t = times[i];
hashed[p]++;
if(hashed[p]>=vote){
if(p!=curr){
curr = p;
votes.push_back(Node(curr,t));
}
if(hashed[p]>vote)
vote = hashed[p];
}
}
}
int q(int t) {
int l = 1, h = votes.size();
while(l<h){
int mid = l + (h-l)/2;
if(votes[mid].time <= t)
l = mid+1;
else
h = mid;
}
return votes[l-1].person;
}
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/