Skip to content

Commit

Permalink
Update online-election.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Sep 23, 2018
1 parent a5e7006 commit a55f9c8
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions C++/online-election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ class TopVotedCandidate {
int lead = -1;
unordered_map<int, int> count;
for (int i = 0; i < persons.size(); ++i) {
lookup_[times[i]] = persons[i];
}
for (auto& kvp : lookup_) {
if (++count[kvp.second] >= count[lead]) {
lead = kvp.second;
if (++count[persons[i]] >= count[lead]) {
lead = persons[i];
lookup_.emplace_back(times[i], persons[i]);
}
kvp.second = lead;
}
}

int q(int t) {
return prev(lookup_.upper_bound(t))->second;
return prev(upper_bound(lookup_.begin(), lookup_.end(),
make_pair(t, numeric_limits<int>::max())))->second;
}

private:
map<int, int> lookup_;
vector<pair<int, int>> lookup_;
};

/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
* int param_1 = obj.q(t);
*/

0 comments on commit a55f9c8

Please sign in to comment.