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. js中字符串转数组,数组转字符串及逆序的方法

    var str = "a,b,c,d,e,f,g";//声明一个字符串 str = str.split(',').reverse();//用split函数拆分成数组对象,再用rev ...

  2. 给<input type="color">设置默认值

    参考:https://stackoverflow.com/questions/14943074/html5-input-colors-default-color?utm_medium=organic& ...

  3. 腾讯云CentOS7.4服务器添加swap分区

    自己的腾讯云服务器搭建的zabbix监控中,提示Lack of free swap space 腾讯的官方说明在这: https://cloud.tencent.com/document/produc ...

  4. [原]编译flightGear

    参考:flightgear编译博客201705 flightGear是三维飞行仿真软件,这个款软件是开源的,我们尝试用其源码完整编译一遍这个工程,并使用它. 它用到里以下扩展库: 空气动力学库:JSB ...

  5. Spark面试相关

    Spark Core面试篇01 随着Spark技术在企业中应用越来越广泛,Spark成为大数据开发必须掌握的技能.前期分享了很多关于Spark的学习视频和文章,为了进一步巩固和掌握Spark,在原有s ...

  6. micro-template改造

    (function ($) { $.fn.razor = function (data) { var render=$(this).data('razor'); if (!render) { var ...

  7. Ubuntu环境下,项目出现:Call to undefined function curl_init() 提示

    原因: 没有开启curl扩展 安装或者开启扩展 ubuntu 执行安装Curl的扩展 sudo apt-get install -y php-curl 同: mysql,redis,curl,等. 摘 ...

  8. Linux下更换jdk和配置环境变量

    目前Linux上安装的是jdk7的java环境,由于项目原因需要升级到jdk8,无需卸载掉原本的jdk7,按如下简单步骤即可: 参考了:https://www.cnblogs.com/jiu0821/ ...

  9. mybatis-generator自动生成代码工具

    1.在项目的配置文件中放入配置文件mybatis-generator-config.xml   根据情况修改下配置 <?xml version="1.0" encoding= ...

  10. [lightoj P1306] Solutions to an Equation

    [lightoj P1306] Solutions to an Equation You have to find the number of solutions of the following e ...