# Leetcode 911 - Online Election

Note:

• Give you a time, we need to find the last timestamp that is less or equal than t.
• It just sounds like binary search right?
• To accelerate this process, we need to precalculate an tops[] array in which each element represents the winner at times[i].
• Detail:
• To prevent dead loop, mid = left + right + 1 / 2). Without plus 1, we might fall into a dead loop like [1, 3], t = 2 coz when times[i] <= t, we have left = mid. In this example, we’ll be stuck at left = 0 forever.

Question:

You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i].

For each query at a time t, find the person that was leading the election at time t. Votes cast at time t will count towards our query. In the case of a tie, the most recent vote (among tied candidates) wins.

Implement the TopVotedCandidate class:

• TopVotedCandidate(int[] persons, int[] times) Initializes the object with the persons and times arrays.
• int q(int t) Returns the number of the person that was leading the election at time t according to the mentioned rules.

Example:

Code: