加载中...
911-在线选举(Online Election)
发表于:2021-12-03 | 分类: 中等
字数统计: 1.6k | 阅读时长: 8分钟 | 阅读量:

原文链接: https://leetcode-cn.com/problems/online-election

英文原文

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.

中文题目

在选举中,第 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. 1 <= persons.length = times.length <= 5000
  2. 0 <= persons[i] <= persons.length
  3. times 是严格递增的数组,所有元素都在 [0, 10^9] 范围中。
  4. 每个测试用例最多调用 10000 次 TopVotedCandidate.q
  5. TopVotedCandidate.q(int t) 被调用时总是满足 t >= times[0]

通过代码

官方题解

方法 1:列表 + 二分搜索

想法和算法

我么可以把选票存储在选票列表的列表 A 中。每个投票都有一个人和一个时间戳,A[count] 是一个列表,记录当前人获得的第 count 张选票。

然后,A[i][0]A[i] 单调增加,所以我们可以利用二分搜索根据时间找到最近的选票。

[]
class TopVotedCandidate { List<List<Vote>> A; public TopVotedCandidate(int[] persons, int[] times) { A = new ArrayList(); Map<Integer, Integer> count = new HashMap(); for (int i = 0; i < persons.length; ++i) { int p = persons[i], t = times[i]; int c = count.getOrDefault(p, 0) + 1; count.put(p, c); while (A.size() <= c) A.add(new ArrayList<Vote>()); A.get(c).add(new Vote(p, t)); } } public int q(int t) { // Binary search on A[i][0].time for smallest i // such that A[i][0].time > t int lo = 1, hi = A.size(); while (lo < hi) { int mi = lo + (hi - lo) / 2; if (A.get(mi).get(0).time <= t) lo = mi + 1; else hi = mi; } int i = lo - 1; // Binary search on A[i][j].time for smallest j // such that A[i][j].time > t lo = 0; hi = A.get(i).size(); while (lo < hi) { int mi = lo + (hi - lo) / 2; if (A.get(i).get(mi).time <= t) lo = mi + 1; else hi = mi; } int j = Math.max(lo-1, 0); return A.get(i).get(j).person; } } class Vote { int person, time; Vote(int p, int t) { person = p; time = t; } }
[]
class TopVotedCandidate(object): def __init__(self, persons, times): self.A = [] self.count = collections.Counter() for p, t in zip(persons, times): self.count[p] = c = self.count[p] + 1 while len(self.A) <= c: self.A.append([]) self.A[c].append((t, p)) def q(self, t): lo, hi = 1, len(self.A) while lo < hi: mi = (lo + hi) / 2 if self.A[mi][0][0] <= t: lo = mi + 1 else: hi = mi i = lo - 1 j = bisect.bisect(self.A[i], (t, float('inf'))) return self.A[i][j-1][1]

复杂度分析

  • 时间复杂度:$O(N + Q \log^2 N)$,其中 $N$ 是选票的个数,$Q$ 是询问的个数。
  • 空间复杂度:$O(N)$。

方法 2:预计算结果 + 二分搜索

想法和算法

每当选票记录,我们可以记录每个胜者改变的 (winner, time) 时刻信息。之后,我们拥有一个有序的时刻信息,并用二分搜索找到答案。

[]
class TopVotedCandidate { List<Vote> A; public TopVotedCandidate(int[] persons, int[] times) { A = new ArrayList(); Map<Integer, Integer> count = new HashMap(); int leader = -1; // current leader int m = 0; // current number of votes for leader for (int i = 0; i < persons.length; ++i) { int p = persons[i], t = times[i]; int c = count.getOrDefault(p, 0) + 1; count.put(p, c); if (c >= m) { if (p != leader) { // lead change leader = p; A.add(new Vote(leader, t)); } if (c > m) m = c; } } } public int q(int t) { int lo = 1, hi = A.size(); while (lo < hi) { int mi = lo + (hi - lo) / 2; if (A.get(mi).time <= t) lo = mi + 1; else hi = mi; } return A.get(lo - 1).person; } } class Vote { int person, time; Vote(int p, int t) { person = p; time = t; } }
[]
class TopVotedCandidate(object): def __init__(self, persons, times): self.A = [] count = collections.Counter() leader, m = None, 0 # leader, num votes for leader for p, t in itertools.izip(persons, times): count[p] += 1 c = count[p] if c >= m: if p != leader: # lead change leader = p self.A.append((t, leader)) if c > m: m = c def q(self, t): i = bisect.bisect(self.A, (t, float('inf')), 1) return self.A[i-1][1]

复杂度分析

  • 时间复杂度:$O(N + Q\log{N})$,其中 $N$ 是选票个数,$Q$ 是询问个数。
  • 空间复杂度:$O(N)$。

统计信息

通过次数 提交次数 AC比率
4918 11465 42.9%

提交历史

提交时间 提交结果 执行时间 内存消耗 语言
上一篇:
910-最小差值 II(Smallest Range II)
下一篇:
913-猫和老鼠(Cat and Mouse)
本文目录
本文目录