Similar Problems

Similar Problems not available

Online Election - Leetcode Solution

Companies:

LeetCode:  Online Election Leetcode Solution

Difficulty: Medium

Topics: design binary-search hash-table array  

The Online Election problem on LeetCode is a medium-level problem that requires implementing an efficient algorithm to solve. The problem statement is as follows:

There are n citizens voting in this year's election. Each voter writes a number on their ballot paper from a range of 0 to n - 1. The number denotes the ID of the candidate they are voting for. The ballot papers are not counted until all the citizens have cast their votes.

The candidate with the most number of votes wins the election. If there are multiple candidates with the same number of votes, the candidate with the lowest ID wins.

Implement the function TopVotedCandidate::TopVotedCandidate(vector<int>& persons, vector<int>& times) where:

  • TopVotedCandidate::TopVotedCandidate(vector<int>& persons, vector<int>& times) Initializes the object with the persons and times arrays.
  • int TopVotedCandidate::q(int t) Returns the ID of the candidate with the most votes at time t.

Online Election Solution Code

1