Leetcode Problem 911. Online Election

911. Online Election

Leetcode Solutions

Precomputed Answer + Binary Search

  1. Initialize a list leaders to store pairs of (leader, time).
  2. Initialize a dictionary vote_counts to keep track of the number of votes for each candidate.
  3. Initialize current_leader to -1 and max_votes to 0.
  4. Iterate through each vote in persons and times: a. Increment the vote count for the current person. b. If the current person's vote count is greater than or equal to max_votes, update current_leader and max_votes. c. Append the pair (current_leader, times[i]) to leaders if the leader changes.
  5. For each query q(t): a. Perform a binary search on leaders to find the rightmost pair where time is less than or equal to t. b. Return the leader from the found pair.
UML Thumbnail

List of Lists + Binary Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...