911. Online Election Medium
1/**
2 * [911] Online Election
3 *
4 * 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].
5 * 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.
6 * Implement the TopVotedCandidate class:
7 *
8 * TopVotedCandidate(int[] persons, int[] times) Initializes the object with the persons and times arrays.
9 * int q(int t) Returns the number of the person that was leading the election at time t according to the mentioned rules.
10 *
11 *
12 * Example 1:
13 *
14 * Input
15 * ["TopVotedCandidate", "q", "q", "q", "q", "q", "q"]
16 * [[[0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]], [3], [12], [25], [15], [24], [8]]
17 * Output
18 * [null, 0, 1, 1, 0, 0, 1]
19 * Explanation
20 * TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
21 * topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
22 * topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
23 * 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.)
24 * topVotedCandidate.q(15); // return 0
25 * topVotedCandidate.q(24); // return 0
26 * topVotedCandidate.q(8); // return 1
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= persons.length <= 5000
32 * times.length == persons.length
33 * 0 <= persons[i] < persons.length
34 * 0 <= times[i] <= 10^9
35 * times is sorted in a strictly increasing order.
36 * times[0] <= t <= 10^9
37 * At most 10^4 calls will be made to q.
38 *
39 */
40pub struct Solution {}
41
42// problem: https://leetcode.com/problems/online-election/
43// discuss: https://leetcode.com/problems/online-election/discuss/?currentPage=1&orderBy=most_votes&query=
44
45// submission codes start here
46
47struct TopVotedCandidate {
48 vec![]
49 }
50
51
52/**
53 * `&self` means the method takes an immutable reference.
54 * If you need a mutable reference, change it to `&mut self` instead.
55 */
56impl TopVotedCandidate {
57
58 fn new(persons: Vec<i32>, times: Vec<i32>) -> Self {
59
60 }
61
62 fn q(&self, t: i32) -> i32 {
63
64 }
65}
66
67/**
68 * Your TopVotedCandidate object will be instantiated and called as such:
69 * let obj = TopVotedCandidate::new(persons, times);
70 * let ret_1: i32 = obj.q(t);
71 */
72
73// submission codes end
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_911() {
81 }
82}
83
Back
© 2025 bowen.ge All Rights Reserved.