Online Election - Problem
Online Election System
You're tasked with implementing a real-time election tracking system! Imagine you're building the backend for a live voting platform where votes come in over time, and news outlets need to query who's leading at any given moment.
You are given two integer arrays:
•
•
The i-th vote was cast for
Your mission: For any query time
Implement the TopVotedCandidate class:
•
•
You're tasked with implementing a real-time election tracking system! Imagine you're building the backend for a live voting platform where votes come in over time, and news outlets need to query who's leading at any given moment.
You are given two integer arrays:
•
persons - the candidates who received votes•
times - when each vote was castThe i-th vote was cast for
persons[i] at time times[i].Your mission: For any query time
t, determine who was leading the election at that moment. Votes cast exactly at time t count toward the result. In case of a tie, the candidate who received the most recent vote (among the tied candidates) wins the tiebreaker.Implement the TopVotedCandidate class:
•
TopVotedCandidate(int[] persons, int[] times) - Initialize with vote data•
int q(int t) - Return the leading candidate at time t Input & Output
example_1.py — Basic Election Tracking
$
Input:
persons = [0, 1, 1, 0, 0, 1, 0], times = [0, 5, 10, 15, 20, 25, 30]
Queries: q(3), q(12), q(25), q(15), q(24), q(8)
›
Output:
0, 1, 1, 0, 0, 1
💡 Note:
At t=3: Only person 0 has votes (1 vote). At t=12: Person 1 has 2 votes, person 0 has 1 vote - person 1 leads. At t=25: Tied at 3 votes each, but person 1's most recent vote was at t=25, person 0's at t=20 - person 1 wins tiebreaker.
example_2.py — Tiebreaker Scenario
$
Input:
persons = [0, 1, 0, 1], times = [0, 10, 20, 30]
Queries: q(15), q(25), q(35)
›
Output:
0, 1, 1
💡 Note:
At t=15: Both have 1 vote each, but person 0's vote is more recent (t=0 vs t=10 - wait, person 1 voted at t=10, so person 1 wins). At t=25: Person 1's last vote at t=10, person 0's at t=20 - person 0 wins. At t=35: Both tied at 2 votes, person 1's most recent vote at t=30 wins.
example_3.py — Single Candidate
$
Input:
persons = [0, 0, 0], times = [1, 2, 3]
Queries: q(0), q(1), q(2), q(4)
›
Output:
0, 0, 0, 0
💡 Note:
Only one candidate exists, so they always win regardless of when we query (as long as there's at least one vote by that time).
Constraints
- 1 ≤ persons.length ≤ 5000
- times.length == persons.length
- 0 ≤ persons[i] ≤ 999
- 1 ≤ times[i] ≤ 109
- times is sorted in strictly increasing order
- At most 104 calls will be made to q
- 1 ≤ t ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Votes arrive chronologically
Process each vote as it comes in during the day
2
Update running totals
Keep track of vote counts for each candidate
3
Determine leader with tiebreaker
Most votes wins; if tied, most recent vote wins
4
Store snapshot
Remember who was leading at this moment
5
Binary search for queries
Quickly find the right moment in time for any query
Key Takeaway
🎯 Key Insight: By precomputing the leader after each vote and using binary search for queries, we transform an O(n) per query problem into O(log n) per query - perfect for real-time election tracking systems!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code