diff --git a/C++/online-election.cpp b/C++/online-election.cpp index ea8fa7af0..a6ececdeb 100644 --- a/C++/online-election.cpp +++ b/C++/online-election.cpp @@ -8,22 +8,20 @@ class TopVotedCandidate { int lead = -1; unordered_map 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::max())))->second; } private: - map lookup_; + vector> lookup_; }; /** @@ -31,4 +29,3 @@ class TopVotedCandidate { * TopVotedCandidate obj = new TopVotedCandidate(persons, times); * int param_1 = obj.q(t); */ -