Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TopVotedCandidate {
- public:
- vector<pair<int, int>> winner;
- unordered_map<int, int> votes;
- TopVotedCandidate(vector<int>& persons, vector<int>& times) {
- int maxVotes = 0, prevWinner;
- for(int i=0; i<persons.size(); i++){
- votes[persons[i]]++;
- if(votes[persons[i]] >= maxVotes){
- maxVotes = votes[persons[i]];
- prevWinner = persons[i];
- }
- winner.push_back({times[i], prevWinner});
- }
- }
- int q(int t) {
- int low = 0, high = winner.size();
- while(low < high){
- int mid = (low+high)/2;
- int curr = winner[mid].first;
- if(t < curr)
- high = mid;
- else
- low = mid+1;
- }
- return winner[low-1].second;
- }
- };
- /**
- * Your TopVotedCandidate object will be instantiated and called as such:
- * TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
- * int param_1 = obj->q(t);
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement