力扣911.在线选举
-
类似1818. 将每个时刻的候选人存储 在时间段上做二分
cpp
class TopVotedCandidate {
vector<int> time,person;
public:
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
int n = persons.size();
unordered_map<int,int> cnt;
int ms = 0,mi = 0;
time = times;
for(int i=0;i<n;i++)
{
if(++cnt[persons[i]] >= ms)
{
ms = cnt[persons[i]];
mi = persons[i];
}
person.push_back(mi);
}
}
int q(int t) {
int idx = ranges::upper_bound(time,t) - time.begin() - 1;
return person[idx];
}
};