911. Online Election #
题目 #
In an election, the i
-th vote was cast for persons[i]
at time times[i]
.
Now, we would like to implement the following query function: TopVotedCandidate.q(int t)
will return the number of 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.
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:
At time 3, the votes are [0], and 0 is leading.
At time 12, the votes are [0,1,1], and 1 is leading.
At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
This continues for 3 more queries at time 15, 24, and 8.
Note:
1 <= persons.length = times.length <= 5000
0 <= persons[i] <= persons.length
times
is a strictly increasing array with all elements in[0, 10^9]
.TopVotedCandidate.q
is called at most10000
times per test case.TopVotedCandidate.q(int t)
is always called witht >= times[0]
.
题目大意 #
在选举中,第 i 张票是在时间为 times[i] 时投给 persons[i] 的。
现在,我们想要实现下面的查询函数: TopVotedCandidate.q(int t) 将返回在 t 时刻主导选举的候选人的编号。
在 t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。
提示:
- 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]。
解题思路 #
- 给出一个 2 个数组,分别代表第
i
人在第t
时刻获得的票数。需要实现一个查询功能的函数,查询在任意t
时刻,输出谁的选票领先。 persons[]
数组里面装的是获得选票人的编号,times[]
数组里面对应的是每个选票的时刻。times[]
数组默认是有序的,从小到大排列。先计算出每个时刻哪个人选票领先,放在一个数组中,实现查询函数的时候,只需要先对times[]
数组二分搜索,找到比查询时间t
小的最大时刻i
,再在选票领先的数组里面输出对应时刻领先的人的编号即可。
代码 #
package leetcode
import (
"sort"
)
// TopVotedCandidate define
type TopVotedCandidate struct {
persons []int
times []int
}
// Constructor911 define
func Constructor911(persons []int, times []int) TopVotedCandidate {
leaders, votes := make([]int, len(persons)), make([]int, len(persons))
leader := persons[0]
for i := 0; i < len(persons); i++ {
p := persons[i]
votes[p]++
if votes[p] >= votes[leader] {
leader = p
}
leaders[i] = leader
}
return TopVotedCandidate{persons: leaders, times: times}
}
// Q define
func (tvc *TopVotedCandidate) Q(t int) int {
i := sort.Search(len(tvc.times), func(p int) bool { return tvc.times[p] > t })
return tvc.persons[i-1]
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* obj := Constructor(persons, times);
* param_1 := obj.Q(t);
*/