0911. Online Election
https://leetcode.com/problems/online-election
Description
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 thepersons
andtimes
arrays.int q(int t)
Returns the number of the person that was leading the election at timet
according to the mentioned rules.
Example 1:
Constraints:
1 <= persons.length <= 5000
times.length == persons.length
0 <= persons[i] < persons.length
0 <= times[i] <= 109
times
is sorted in a strictly increasing order.times[0] <= t <= 109
At most
104
calls will be made toq
.
ac
Last updated