-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonlineElection.cpp
More file actions
29 lines (26 loc) · 821 Bytes
/
onlineElection.cpp
File metadata and controls
29 lines (26 loc) · 821 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
// Source: https://leetcode.com/problems/online-election/
// Author: Miao Zhang
// Date: 2021-03-24
class TopVotedCandidate {
public:
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
vector<int> votes(persons.size() + 1);
int last_lead = persons.front();
for (int i = 0; i < persons.size(); i++) {
if (++votes[persons[i]] >= votes[last_lead]) {
last_lead = persons[i];
}
leads_[times[i]] = last_lead;
}
}
int q(int t) {
return prev(leads_.upper_bound(t))->second;
}
private:
map<int, int> leads_; // time->lead
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/