911.在线选举:二分运用题

题目描述

这是 LeetCode 上的 911. 在线选举 ,难度为 中等

Tag : 「二分」

给你两个整数数组

personspersons

timestimes

在选举中,第 

ii

 张票是在时刻为 

times[i]times[i]

 时投给候选人

persons[i]persons[i]

 的。

对于发生在时刻

tt

的每个查询,需要找出在 t 时刻在选举中领先的候选人的编号。

在 

tt

时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。

实现 TopVotedCandidate 类:

  • TopVotedCandidate(int[] persons, int[] times) 使用 
    personspersons

    timestimes

    数组初始化对象。

  • int q(int t) 根据前面描述的规则,返回在时刻
    tt

    在选举中领先的候选人的编号。

示例:

输入:
["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]

解释:
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // 返回 0 ,在时刻 3 ,票数分布为 [0] ,编号为 0 的候选人领先。
topVotedCandidate.q(12); // 返回 1 ,在时刻 12 ,票数分布为 [0,1,1] ,编号为 1 的候选人领先。
topVotedCandidate.q(25); // 返回 1 ,在时刻 25 ,票数分布为 [0,1,1,0,0,1] ,编号为 1 的候选人领先。(在平局的情况下,1 是最近获得投票的候选人)。
topVotedCandidate.q(15); // 返回 0
topVotedCandidate.q(24); // 返回 0
topVotedCandidate.q(8); // 返回 1
复制代码

提示:


  • 1<=persons.length<=50001 <= persons.length <= 5000


  • times.length==persons.lengthtimes.length == persons.length


  • 0<=persons[i]<persons.length0 <= persons[i] < persons.length


  • 0<=times[i]<=1090 <= times[i] <= 10^9


  • timestimes

    是一个严格递增的有序数组


  • times[0]<=t<=109times[0] <= t <= 10^9

  • 每个测试用例最多调用
    10410^4

    q

二分

根据题意,我们会在

times[i]times[i]

时刻为

persons[i]persons[i]

候选人增加一票。

利用

timestimes

数组严格递增,我们可以在处理

timestimes

时(模拟加票过程),使用一个变量

valval

来维护当前得票的最大数量,使用

listlist

来记录不同时刻点的候选人交替情况。

起始时

val=0val = 0

valval

时,我们往

listlist

追加二元组记录

list[idx]=(times[i],persons[i])list[idx] = (times[i], persons[i])

valval

每个

list[idx]list[idx]

代表了当前候选人

list[idx][1]list[idx][1]

的首次当选时刻为

list[idx][0]list[idx][0]

举个 🌰,若

i=list[idx][0],j=list[idx+1][0]i = list[idx][0], j = list[idx + 1][0]

[i,j)[i, j)

内当选的候选人为

list[idx][1]list[idx][1]

在查询时,我们只需要通过「二分」找到

listlist

中满足

list[i][0]<=tlist[i][0] <= t

rr

(最大下标),取

list[r][1]list[r][1]

即是答案。

代码:

class TopVotedCandidate {
    List<int[]> list = new ArrayList<>();
    public TopVotedCandidate(int[] persons, int[] times) {
        int val = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < times.length; i++) {
            map.put(persons[i], map.getOrDefault(persons[i], 0) + 1);
            if (map.get(persons[i]) >= val) {
                val = map.get(persons[i]);
                list.add(new int[]{times[i], persons[i]});
            }
        }
    }
    public int q(int t) {
        int l = 0, r = list.size() - 1;
        while (l < r) {
            int mid = l + r + 1 >> 1;
            if (list.get(mid)[0] <= t) l = mid;
            else r = mid - 1;
        }
        return list.get(r)[0] <= t ? list.get(r)[1] : 0;
    }
}
复制代码
  • 时间复杂度:令
    nn

    为数组长度,

    mm

    为查询次数(调用 q 的次数)。预处理出

    listlist

    的复杂度为

    O(n)O(n)

    ;最坏情况下,每个

    time[i]time[i]

    时刻都会交替产生新的候选人编号,即最坏情况下

    listlist

    的数组长度为

    nn

    ,总的查询复杂度为

    O(mlogn)O(m * \log{n})

  • 空间复杂度:
    O(n)O(n)

最后

这是我们「刷穿 LeetCode」系列文章的第 No.911 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:github.com/SharingSour…

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。