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 most 10000 times per test case.
TopVotedCandidate.q(int t) is always called with t >= times[0].

所以这个题使用List保存每个时间点对应的当前的获得票数最多的person。在q(t)中,使用二分查找到第一个小于t的times位置,然后返回这个位置对应的时间得票最多的person即可。

平均的时间复杂度是O(logn),空间复杂度是O(N).

class TopVotedCandidate {
//persons [0,1,1,0,0,1,0]
//times [0,5,10,15,20,25,30]
List<int[]> list; public TopVotedCandidate(int[] persons, int[] times) {
list = new ArrayList<>();
Map<Integer, Integer> map = new HashMap<>();
int lead = -1;
int leadP = -1;
for (int i = 0; i < persons.length; i++){
map.put(persons[i], map.getOrDefault(persons[i],0)+1);
int[] pair = new int[2];
pair[0] = times[i]; if(map.get(persons[i]) >= lead){
lead = map.get(persons[i]);
leadP=persons[i];
}
pair[1] = leadP;
list.add(pair);
}
} public int q(int t) {
int l = 0;
int r = list.size()-1;
while(l <= r){
int mid = l + (r-l)/2;
if(t == list.get(mid)[0]){
return list.get(mid)[1];
}
else if(t < list.get(mid)[0]){
r = mid-1;
}
else{
l = mid+1;
}
}
return list.get(r)[1]; }
} /**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
* int param_1 = obj.q(t);
*/

LeetCode - Online Election的更多相关文章

  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. LeetCode 911. Online Election

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

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

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

  4. Swift LeetCode 目录 | Catalog

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

  5. [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 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  8. [译]ZOOKEEPER RECIPES-Leader Election

    选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. HDU4745--区间DP+回文串

    这题的题意为,给你一个环状的字符串,有两只兔子分别从某任意的石头上开始跳跃.一只顺时针跳.一只逆时针跳.两只兔子每一次落脚处石头的质量都相同.兔子要一步一步的跳,且不能跳到之前跳到过的地方.总的来说, ...

  2. zabbix-3.4.14源码安装

    此次介绍一下,zabbix的源码安装,因为zabbix的依赖很多,所以源码安装有很多依赖需要安装 首先,下载zabbix源码包 wget https://nchc.dl.sourceforge.net ...

  3. 智能合约遇到的小错误 network up to date解决办法

    https://blog.csdn.net/qindong564950620/article/details/68933678 说 network up to date .这个错误我刚开始不知道怎么解 ...

  4. sqldeveloper 重置java.exe路径方法

    sqldeveloper重新配置java.exe 1.进入D:\app\product\11.2.0\dbhome_1\sqldeveloper\sqldeveloper\bin路径下,找到sqlde ...

  5. Hbasewindows系统下启动报错及解决办法

    今天在本地windows电脑上,装pinpoint时,需要先安装一个Hbase数据库,按照教程下载启动Hbase数据库时,却启动报错:java.io.IOException: Could not lo ...

  6. Rancher2.0中部署Longhorn分布式存储实验

    目录 1.简介 2.实验环境 3.应用商店中部署longhorn 4.创建工作负载,使用longhorn存储 5.查看longhorn UI 6.注意事项 1.简介: Longhorn是Rancher ...

  7. legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)

    legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法) 一.总结 一句话总结:会执行的,所以写base控制器里面的方法要注意,base控制器里面的方法要以查数据为 ...

  8. js中defer实现等文档加载完在执行脚本

    我们可以使用defer来实现类似window.onload的功能: <script src="../CGI-bin/delscript.js" defer></s ...

  9. Loadrunner常见的乱码问题

    1.录制的脚本出现了乱码 录制的时候出现乱码,如果不影响回放,我们可以不管它,如果影响回放结果,我们可以使用以下方法解决:     1)更改录制选项         选择菜单栏Tools---> ...

  10. [noip2017] 前三周总结

    [noip2017] 前三周总结 10.20 Fri. Day -21 距离noip复赛还有3周了,进行最后的冲刺! 首先要说今天过得并不好,和我早上比赛打挂了有关系. 不过每一次比赛都能暴露出我的漏 ...