forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
33 lines (30 loc) · 811 Bytes
/
Solution.cpp
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:
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
int n = persons.size();
this->times = times;
wins.resize(n);
vector<int> cnt(n);
int cur = 0;
for (int i = 0; i < n; ++i) {
int p = persons[i];
++cnt[p];
if (cnt[cur] <= cnt[p]) {
cur = p;
}
wins[i] = cur;
}
}
int q(int t) {
int i = upper_bound(times.begin(), times.end(), t) - times.begin() - 1;
return wins[i];
}
private:
vector<int> times;
vector<int> wins;
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/