> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Online Election Python Solution with Tests

> Tested Python solution for LeetCode 911 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 911, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Binary Search](/catalog/topics/binary-search), [Design](/catalog/topics/design). [View on LeetCode](https://leetcode.com/problems/online-election/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 911   # by problem number
lcpy gen -s online_election   # by problem name
```

## Problem

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 the number of the person that was leading the election at time `t` according to the mentioned rules.

### Examples

```
Input
["TopVotedCandidate", "q", "q", "q", "q", "q", "q"]
[[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
Output
[null, 0, 1, 1, 0, 0, 1]

Explanation
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
topVotedCandidate.q(15); // return 0
topVotedCandidate.q(24); // return 0
topVotedCandidate.q(8); // return 1
```

### Constraints

* `1 <= persons.length <= 5000`
* `times.length == persons.length`
* `0 <= persons[i] < persons.length`
* `0 <= times[i] <= 10^9`
* `times` is sorted in a strictly increasing order.
* `times[0] <= t <= 10^9`
* At most `10^4` calls will be made to `q`.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/online_election/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/online_election/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from bisect import bisect_right


class TopVotedCandidate:
    # Time: O(n) to build the leader timeline
    # Space: O(n) for the leader timeline
    def __init__(self, persons: list[int], times: list[int]) -> None:
        self.times = times
        self.leaders: list[int] = []
        counts: dict[int, int] = {}
        leader = -1
        for person in persons:
            counts[person] = counts.get(person, 0) + 1
            if counts[person] >= counts.get(leader, 0):
                leader = person
            self.leaders.append(leader)

    # Time: O(log n)
    # Space: O(1)
    def q(self, t: int) -> int:
        return self.leaders[bisect_right(self.times, t) - 1]
```

## Complexity

| Time | Space |
| - | - |
| O(n) to build the leader timeline | O(n) for the leader timeline |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.