Online Election - Problem

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.

Input & Output

Example 1 — Basic Election Tracking
$ Input: persons = [0,1,1,0,0,1,0], times = [0,5,10,15,20,25,30], queries = [3,12,25,15,24,8]
Output: [0,1,1,0,0,1]
💡 Note: At time 3: person 0 leads (1-0). At time 12: person 1 leads (2-1). At time 25: person 1 leads (3-3, but person 1 voted most recently). At time 15: person 0 leads (2-2, but person 0 voted most recently at time 15).
Example 2 — Early Query
$ Input: persons = [0,1,1,0], times = [0,5,10,15], queries = [2,8]
Output: [0,1]
💡 Note: At time 2: only person 0 has voted. At time 8: person 1 leads 2-1.
Example 3 — Tie Breaking
$ Input: persons = [0,1,0,1], times = [0,5,10,15], queries = [10,15]
Output: [0,1]
💡 Note: At time 10: tie 2-2, person 0 voted most recently at time 10. At time 15: tie 2-2, person 1 voted most recently at time 15.

Constraints

  • 1 ≤ persons.length ≤ 5000
  • times.length == persons.length
  • 0 ≤ persons[i] ≤ 999
  • 1 ≤ times[i] ≤ 109
  • times is sorted in a strictly increasing order
  • 1 ≤ queries.length ≤ 104
  • 1 ≤ queries[i] ≤ 109

Visualization

Tap to expand
Online Election Problem INPUT persons[]: 0 1 1 0 0 1 0 times[]: 0 5 10 15 20 25 30 queries[]: 3 12 25 15 24 8 Vote Timeline: 0 t=0 1 t=5 1 t=10 0 t=15 0 t=20 1 t=25 Person 0 Person 1 ALGORITHM STEPS 1 Precompute Leaders Track leader at each time Time: 0 5 10 15 20 25 Leader: 0 1 1 0 0 1 2 Count Votes Update counts, track max 3 Binary Search Find latest time <= query t Query t=12: 10 <= 12 --> Leader=1 4 Handle Ties Most recent vote wins tie At t=5: P0=1, P1=1 P1 voted last --> P1 leads Complexity: Init: O(n), Query: O(log n) FINAL RESULT Query Time Found Leader t=3 0 0 t=12 10 1 t=25 25 1 t=15 15 0 t=24 20 0 t=8 5 1 Output Array: [0, 1, 1, 0, 0, 1] OK - Verified Query Flow: Query t BinSearch Leader Key Insight: Precompute the leader at each voting time during initialization. Store leaders[] array where leaders[i] is the winner at times[i]. For queries, use binary search to find the largest time <= query time t, then return the precomputed leader. Ties favor the most recent vote (latest voter wins). TutorialsPoint - Online Election | Optimal Solution (Binary Search + Precomputation)
Asked in
Google 15 Facebook 12 Microsoft 8
28.0K Views
Medium Frequency
~25 min Avg. Time
1.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen