-
Notifications
You must be signed in to change notification settings - Fork 3
/
911.cpp
35 lines (33 loc) · 1 KB
/
911.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
34
35
class TopVotedCandidate {
public:
vector<int> precomputed;
vector<int> times;
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
this->times = times;
unordered_map<int, int> person2votes;
int n = times.size();
int maxVotes = 0;
precomputed.resize(n, -1);
for (int i = 0; i < n; ++i) {
person2votes[persons[i]]++;
if (person2votes[persons[i]] >= maxVotes) {
maxVotes = person2votes[persons[i]];
precomputed[i] = persons[i];
}
else {
precomputed[i] = precomputed[i - 1];
}
}
}
int q(int t) {
auto it = upper_bound(times.begin(), times.end(), t);
it--;
int index = it - times.begin();
return precomputed[index];
}
};
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/