【LeetCode每日一题】【2021/12/11】911. 在线选举

该博客介绍了如何使用哈希表和二分查找解决LeetCode中911题的在线选举问题。在选举过程中,通过维护票数领先的候选人及其出现时间,利用二分查找高效地回答在特定时刻领先的候选人。文章详细解析了算法思路和实现过程,并提供了复杂度分析。
摘要由CSDN通过智能技术生成


911. 在线选举

LeetCode: 911. 在线选举

中 等 \color{#FFB800}{中等}

给你两个整数数组 personstimes 。在选举中,第 i 张票是在时刻为 times[i] 时投给候选人 persons[i] 的。
对于发生在时刻 t 的每个查询,需要找出在 t 时刻在选举中领先的候选人的编号。
t 时刻投出的选票也将被计入我们的查询之中。在平局的情况下,最近获得投票的候选人将会获胜。
实现 TopVotedCandidate 类:

  • TopVotedCandidate(int[] persons, int[] times) 使用 personstimes 数组初始化对象。
  • int q(int 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]

解释:
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 <= 5000
  • times.length == persons.length
  • 0 <= persons[i] < persons.length
  • 0 <= times[i] <= 109
  • times 是一个严格递增的有序数组
  • times[0] <= t <= 109
  • 每个测试用例最多调用 104q

方法1:哈希表 + 二分查找

我们需要对每个时刻领先的候选人进行预计算,这样在调用 q 函数的时候只要进行查找即可。

定义哈希表 total,记录每个时刻票数领先的候选人存入 ahead 数组。按题意,如果有多人获得了相等的最高票数,则选择最近那位获得最高票数的。因此我们可以定义一个 top 指针,指向最近获得最高票数的候选人。遍历 persons 数组时,每位候选人的票数自增,total[persons[i]]++,同时,如果自增后 total[persons[i]] >= total[top],就将 top 指向这位候选人,top=persons[i]

每个时刻票数最高的候选人已经确定下来了,现在需要解决查找的问题。假设 times 数组为 [5,10],查询时刻 8。由于时刻 8 还没到时刻 10,因此时刻 8 时的票数最高者就是时刻 5 时的票数最高者。同时,times 数组是严格递增的数组,因此查找的方法可以选择二分查找

#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
class TopVotedCandidate
{
private:
    vector<int> times;
    const int n;
    int *ahead;

public:
    TopVotedCandidate(vector<int> &persons, vector<int> &times) : times{times}, n{int(times.size())}, ahead{new int[times.size()]}
    {
        unordered_map<int, int> total;

        int *p = ahead;
        int top = -1;
        total[top] = -1;
        for (const int &person : persons)
        {
            total[person]++;

            if (total[person] >= total[top])
            {
                top = person;
            }

            *p = top;
            p++;
        }
    }

    int q(int t)
    {
        int left = 0, right = this->n - 1;
        while (left < right)
        {
            int mid = ((right - left) >> 1) + left + 1;
            if (this->times[mid] <= t)
            {
                left = mid;
            }
            else
            {
                right = mid - 1;
            }
        }
        return this->ahead[left];
    }

    ~TopVotedCandidate()
    {
        delete[] this->ahead;
    }
};

复杂度分析

  • 时间复杂度: O ( n ) O(n) O(n)。预处理哈希表时要遍历整个数组,时间复杂度为 O ( n ) O(n) O(n);二分查找的时间复杂度为 O ( log ⁡ n ) O(\log n) O(logn)。因此总时间复杂度为 O ( n + log ⁡ n ) = O ( n ) O(n+\log n)=O(n) O(n+logn)=O(n)

  • 空间复杂度: O ( n ) O(n) O(n)。同上。

参考结果

Accepted
97/97 cases passed (260 ms)
Your runtime beats 33.99 % of cpp submissions
Your memory usage beats 93.1 % of cpp submissions (100.4 MB)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亡心灵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值