Advertisement
nikunjsoni

911

May 12th, 2021
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. class TopVotedCandidate {
  2. public:
  3.     vector<pair<int, int>> winner;
  4.     unordered_map<int, int> votes;
  5.     TopVotedCandidate(vector<int>& persons, vector<int>& times) {
  6.         int maxVotes = 0, prevWinner;
  7.         for(int i=0; i<persons.size(); i++){
  8.             votes[persons[i]]++;
  9.             if(votes[persons[i]] >= maxVotes){
  10.                 maxVotes = votes[persons[i]];
  11.                 prevWinner = persons[i];
  12.             }
  13.             winner.push_back({times[i], prevWinner});
  14.         }
  15.     }
  16.    
  17.     int q(int t) {
  18.         int low = 0, high = winner.size();
  19.         while(low < high){
  20.             int mid = (low+high)/2;
  21.             int curr = winner[mid].first;
  22.             if(t < curr)
  23.                 high = mid;
  24.             else
  25.                 low = mid+1;
  26.         }
  27.         return winner[low-1].second;
  28.     }
  29. };
  30.  
  31. /**
  32.  * Your TopVotedCandidate object will be instantiated and called as such:
  33.  * TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
  34.  * int param_1 = obj->q(t);
  35.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement