LeetCode #911 Online Election 在線選舉 911 Online Election 在線選舉

911 Online Election 在線選舉

Description:
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:

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] <= 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.

題目描述:
在選舉中,第 i 張票是在時間爲 times[i] 時投給 persons[i] 的。

現在,我們想要實現下面的查詢函數: TopVotedCandidate.q(int t) 將返回在 t 時刻主導選舉的候選人的編號。

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

示例 :

輸入:["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]]
輸出:[null,0,1,1,0,0,1]
解釋:
時間爲 3,票數分佈情況是 [0],編號爲 0 的候選人領先。
時間爲 12,票數分佈情況是 [0,1,1],編號爲 1 的候選人領先。
時間爲 25,票數分佈情況是 [0,1,1,0,0,1],編號爲 1 的候選人領先(因爲最近的投票結果是平局)。
在時間 15、24 和 8 處繼續執行 3 個查詢。

提示:

1 <= persons.length = times.length <= 5000
0 <= persons[i] <= persons.length
times 是嚴格遞增的數組,所有元素都在 [0, 10^9] 範圍中。
每個測試用例最多調用 10000 次 TopVotedCandidate.q。
TopVotedCandidate.q(int t) 被調用時總是滿足 t >= times[0]。

思路:

二分查找
預處理所有的時間的獲勝者
按照時間使用二分查找找到當前時間的獲勝者
C++ 找到 map.upper_bound 的前一個指針
Java 使用 TreeMap.floorEntry()
Python 找到 bisect.bisect_right() 的前一個指針
時間複雜度爲 O(n + mlgn), 空間複雜度爲 O(n), 預處理需要 O(n) 時間, 每個搜索需要 O(n) 時間, n 爲 persons(times) 數組的長度, m 爲查詢的次數

代碼:
C++:

class TopVotedCandidate 
{
private:
    map<int, int> m;
public:
    TopVotedCandidate(vector<int>& persons, vector<int>& times) 
    {
        int n = persons.size(), max_value = 0;
        vector<int> tickets(n);
        for (int i = 0; i < n; i++) m[times[i]] = max_value = (++tickets[persons[i]] >= tickets[max_value] ? persons[i] : max_value);
    }
    
    int q(int t) 
    {
        return (--m.upper_bound(t)) -> second;
    }
};

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

Java:

class TopVotedCandidate {
    private TreeMap<Integer, Integer> map;
    
    public TopVotedCandidate(int[] persons, int[] times) {
        map = new TreeMap<>();
        int n = persons.length, maxValue = 0, tickets[] = new int[n];
        for (int i = 0; i < n; i++) map.put(times[i], maxValue = (++tickets[persons[i]] >= tickets[maxValue] ? persons[i] : maxValue));
    }
    
    public int q(int t) {
        return map.floorEntry(t).getValue();
    }
}

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

Python:

class TopVotedCandidate:

    def __init__(self, persons: List[int], times: List[int]):
        self.times, self.winner, d, max_value, cur_winner = times, [], defaultdict(int), 0, persons[0]
        for person in persons:
            d[person] += 1
            if d[person] >= max_value:
                cur_winner = person
                max_value = d[person]
            self.winner.append(cur_winner)


    def q(self, t: int) -> int:
        return self.winner[bisect_right(self.times, t) - 1]


# Your TopVotedCandidate object will be instantiated and called as such:
# obj = TopVotedCandidate(persons, times)
# param_1 = obj.q(t)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章