来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/online-election

题目描述

给你两个整数数组 persons 和 times 。在选举中,第 i 张票是在时刻为 times[i] 时投给候选人 persons[i] 的。

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

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

实现 TopVotedCandidate 类:

TopVotedCandidate(int[] persons, int[] times) 使用 persons 和 times 数组初始化对象。
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
每个测试用例最多调用 104 次 q

解题思路

本题采用预处理+二分法来求解,观察题目提示,times数组和persons数组大小仅为5000,可以作为突破口,将每个时间段的领先候选人记录下来,然后判断查询时候的时间处于哪个时间段内,直接将领先候选人的编号返回。利用计数的方法将所有数据预处理,将领先的候选人存入tops中,tops[i]代表在tims[i]到time[i+1]内领先候选人的编号,查询时候,利用二分库函数upper_bound找到t所在的时间段time[t],直接返回tops[t]。

源码展示

class TopVotedCandidate {
public:
vector<int> mTops;
vector<int> mTimes;
TopVotedCandidate(vector<int>& persons, vector<int>& times) {
mTops.resize(times.size());
mTimes = times;
unordered_map<int, int> mapiiCount;
int iMax = 0, iPerson = -1;
for(int i = 0; i < persons.size(); i++)
{
mapiiCount[persons[i]]++;
if(iMax <= mapiiCount[persons[i]])
{
iMax = mapiiCount[persons[i]];
iPerson = persons[i];
}
mTops[i] = iPerson;
}
} int q(int t) {
int iPos = upper_bound(mTimes.begin(), mTimes.end(), t) - mTimes.begin() - 1;
return mTops[iPos];
}
}; /**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/

运行结果

LeetCode-911 在线选举的更多相关文章

  1. [LeetCode] 911. Online Election 在线选举

    In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...

  2. [Swift]LeetCode911. 在线选举 | Online Election

    In an election, the i-th vote was cast for persons[i] at time times[i]. Now, we would like to implem ...

  3. LeetCode 911. Online Election

    原题链接在这里:https://leetcode.com/problems/online-election/ 题目: In an election, the i-th vote was cast fo ...

  4. 牛客网/LeetCode/七月在线/HelloWorld114

    除了知乎,还有这些网站与offer/内推/秋招/春招相关. 其中HelloWorld114更是囊括许多IT知识. 当然,我们可以拓宽思考的维度,既然课堂上的老师讲不好,我们可以自己找资源啊= => ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. LeetCode刷题总结-二分查找和贪心法篇

    本文介绍LeetCode上有关二分查找和贪心法的算法题,推荐刷题总数为16道.具体考点归纳如下: 一.二分查找 1.数学问题 题号:29. 两数相除,难度中等 题号:668. 乘法表中第k小的数,难度 ...

  7. 学习笔记之LeetCode

    LeetCode Online Judge https://leetcode.com/ Leetcode:在线编程网站-各大IT公司的笔试面试题 http://blog.csdn.net/huixin ...

  8. Leetcode多线程题库练习(新功能尝鲜)& 个人感悟

    大家好, 我是方子龙.很久没有自己写文章了. 一面是因为工作上的需求开发任务比较重,下班回家基本上就躺床玩几把王者,度过闲暇时光. 二面是一有点时间就自己主动地去看书和学习,知道自己还缺少很多知识,由 ...

  9. 经验分享 | 如何拿到自己满意的offer?

    本文阅读时间约16分钟 最近两年,人工智能(AI)就像一个点石成金的神器,所有的行业,创业公司,或是求职,只要沾着这个词,多少有点脚踩五彩祥云的感觉,故事来了,融资来了,高薪来了. 于是,越来越多的人 ...

  10. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. C#-将进程注册为子进程,父进程崩溃的时候子进程也随之退出的方案和实例

    C#-将进程注册为子进程,父进程崩溃的时候子进程也随之退出的方案和实例 Kill child process when parent process is killed 我正在使用我的应用程序中的Sy ...

  2. Python怎么引入不同的库?

    怎么引入不同的库? 在线安装库 1)pip install 模块名 2)国内源: 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirr ...

  3. Linux基础第一章 概述

    第一章 概述 1.1 前言 本章讨论系统的概念,从硬件.操作系统角度更加深刻的理解计算机系统,并快速浏览Linux系统提供的服务. 1.2 系统组成     1.3 操作系统和应用程序 操作系统这个词 ...

  4. 用了这么多年的 SpringBoot 你知道什么是 SpringBoot 的 Web 类型推断吗?

    用了这么多年的 SpringBoot 那么你知道什么是 SpringBoot 的 web 类型推断吗? 估计很多小伙伴都不知道,毕竟平时开发做项目的时候做的都是普通的 web 项目并不需要什么特别的了 ...

  5. 对一个序列双重argsort的含义

    学习笔记:由numpy.argsort()引发的思考 一.numpy.argsort() 函数定义 函数的定义 首先函数的定义比较简洁: argsort()函数是将x中的元素从小到大排列,提取其对应的 ...

  6. RestTemplate获取json数组

    1.需求描述 接口返回的是一个json数组,要获取到接口返回值并用实体类list接住. 2.解决方法 使用springboot框间自带的Http的工具类RestTemplate调接口,其返回值用hut ...

  7. day14-功能实现13

    家居网购项目实现013 以下皆为部分代码,详见 https://github.com/liyuelian/furniture_mall.git 32.功能30-会员不能登录后台管理 32.1需求分析/ ...

  8. 迁移学习(DANN)《Domain-Adversarial Training of Neural Networks》

    论文信息 论文标题:Domain-Adversarial Training of Neural Networks论文作者:Yaroslav Ganin, Evgeniya Ustinova, Hana ...

  9. vulnhub靶场之RIPPER: 1

    准备: 攻击机:虚拟机kali.本机win10. 靶机:Ripper: 1,下载地址:https://download.vulnhub.com/ripper/Ripper.ova,下载后直接vbox打 ...

  10. GF_CLR初始用 - 正式版

    参照:DeerGF_Wolong框架使用教程 与tackor老哥的踩坑日记所编写,第二次尝试,总结第一次经验重新来. 点击链接加入群聊[Gf_Wolong热更集合] 一. 部署 HybridCLR(W ...