[LeetCode] 911. Online Election

CNoodle發表於2024-11-16

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

Example 1:
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] <= 109
times is sorted in a strictly increasing order.
times[0] <= t <= 109
At most 104 calls will be made to q.

線上選舉。

給你兩個整數陣列 persons 和 times 。在選舉中,第 i 張票是在時刻為 times[i] 時投給候選人 persons[i] 的。

對於發生在時刻 t 的每個查詢,需要找出在 t 時刻在選舉中領先的候選人的編號。

在 t 時刻投出的選票也將被計入我們的查詢之中。在平局的情況下,最近獲得投票的候選人將會獲勝。

實現 TopVotedCandidate 類:
TopVotedCandidate(int[] persons, int[] times) 使用 persons 和 times 陣列初始化物件。
int q(int t) 根據前面描述的規則,返回在時刻 t 在選舉中領先的候選人的編號。

思路

這是一道設計題。這裡我重新解釋一下題意。在選舉中,第 i 張票是在時刻為 times[i] 時投給候選人 persons[i] 的,這個部分應該沒有歧義。但是接下來,對於發生在時刻 t 的每個查詢,請注意這個時刻 t 未必跟投票的時刻是能對的上的。比如題目給的例子,如下是分別在時間點 0, 5, 10, 15, 20, 25, 30 時給不同的 candidate 投票的情況。

[0, 1, 1, 0, 0, 1, 0]
[0, 5, 10, 15, 20, 25, 30]

但是查詢的時刻 t 則分別發生在 3, 12, 25, 15, 24, 8 這幾個時刻。

存投票的結果這部分不難,這道題難在如何高效地查詢。思路是二分法。存投票這個動作,我們需要一個 hashmap 和一個 list。其中 hashmap 存的是每個 candidate 和他們各自對應的票數。list 裡存的是int[] { time, leadingPerson },意思是在每當有一個人投票之後,我們就看一下當前這次投票結束之後領先的候選人是誰,把這個資訊存到 list 裡。

因為投票的時刻 time 是有序的,所以 list 裡的元素也是按 time 有序的。所以當我們查詢的時候就可以用二分法了,這裡相當於是在一個有序的陣列裡找一個最大的小於等於 t 的元素

複雜度

時間O(logn)
空間O(n)

程式碼

Java實現

class TopVotedCandidate {
	List<int[]> list = new ArrayList<>();
	HashMap<Integer, Integer> map = new HashMap<>();
	int max = 0;

    public TopVotedCandidate(int[] persons, int[] times) {
		int n = persons.length;
		for (int i = 0; i < n; i++) {
			int person = persons[i];
			int time = times[i];
			map.put(person, map.getOrDefault(person, 0) + 1);
			if (map.get(person) >= max) {
				max = map.get(person);
				list.add(new int[] {time, person});
			}
		}
    }
    
    public int q(int t) {
        int left = 0;
		int right = list.size() - 1;
		while (left + 1 < right) {
			int mid = left + (right - left) / 2;
            if (list.get(mid)[0] == t) {
                return list.get(mid)[1];
            } else if (list.get(mid)[0] > t) {
				right = mid;
			} else {
                left = mid;
            }
		}
		if (list.get(right)[0] <= t) {
			return list.get(right)[1];
		}
		return list.get(left)[1];
    }
}

/**
 * Your TopVotedCandidate object will be instantiated and called as such:
 * TopVotedCandidate obj = new TopVotedCandidate(persons, times);
 * int param_1 = obj.q(t);
 */

相關文章