Advertisement

Online Election - LeetCode 911 Solution

Online Election - Complete Solution Guide

Online Election is LeetCode problem 911, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two integer arrays persons and times . In an election, the i th 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

Detailed Explanation

The problem simulates an online election where votes are cast at specific times. We are given two arrays, `persons` and `times`, representing the person voted for and the time the vote was cast, respectively. The goal is to implement a `TopVotedCandidate` class that allows us to query who was leading the election at any given time `t`. The votes cast at time `t` *are* counted for the query. In case of a tie, the most recent vote (among the tied candidates) wins. The class has two methods: a constructor `TopVotedCandidate(persons, times)` to initialize with the vote data, and a query method `q(t)` that returns the leading candidate at time `t`.

Solution Approach

The solution pre-computes the leader at each time a vote is cast. During initialization, we iterate through the `persons` and `times` arrays, keeping track of the vote count for each person. As we iterate, we determine the current leader and append it to the `leaders` array. When a query `q(t)` is made, we use binary search on the `times` array to find the correct time index corresponding to time `t`. Because we need the vote at time `t` to be counted, we perform a 'right' binary search to find the rightmost time `t`. Once we have that index, we decrement it by 1 to find the appropriate leader in our `leaders` array.

Step-by-Step Algorithm

  1. Step 1: **Initialization (`__init__`)**: Create arrays to store `times` and `leaders`.
  2. Step 2: **Initialization**: Create a dictionary/map `counts` to store vote counts for each person.
  3. Step 3: **Initialization**: Initialize `leader` to -1 and `max_votes` to 0.
  4. Step 4: **Initialization**: Iterate through the `persons` array. For each `person`, increment their count in the `counts` dictionary.
  5. Step 5: **Initialization**: If the current `person`'s vote count is greater than or equal to `max_votes`, update `max_votes` and set the current `person` as the `leader`.
  6. Step 6: **Initialization**: Append the current `leader` to the `leaders` array after each vote is processed.
  7. Step 7: **Query (`q(t)`)**: Use binary search (`bisect_right` in Python, `upper_bound` in C++, custom `binarySearchRight` in Java, equivalent implementation in C) on the `times` array to find the index of the first time strictly *greater* than `t`.
  8. Step 8: **Query**: Return the element at index `idx - 1` from the `leaders` array. This is the leader at time `t`.

Key Insights

  • Insight 1: Pre-computation of the leaders at each time point is crucial for efficient querying. Instead of re-calculating the leader for every query, we calculate it once during initialization.
  • Insight 2: Binary search is the correct approach for efficiently finding the correct time index in the `times` array for a given query time `t`. Because the `times` array is strictly increasing, it is perfect for binary search.
  • Insight 3: The problem emphasizes that a vote at time `t` is included in the query. This means we need to use `bisect_right` or its equivalent to find the index of the first time strictly *greater* than `t`, and then decrement that index to get the correct time point to determine the leader.

Complexity Analysis

Time Complexity: O(log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Binary Search, Design.

Companies

Asked at: Atlassian, CARS24.