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. CSS布局学习(三) - position属性定义及解释(官网直译)

    static ①元素的位置是在文档正常布局流中的位置. ②设置top right bottom left与z-index无效. ③在未指定position时,static是默认值 以下例子进行说明: ...

  2. 《Visual C# 从入门到精通》第一章使用变量、操作符和表达式——读书笔记

    前言: 这个笔记是我个人总结,主要是熟练自己查看<Visual C# 从入门到精通>(第8版)这本书时,懵然起总结的想法,只是总结一些知识点,在工作项目会用得上,但是对毫无C#语言基础的, ...

  3. Java内存分配机制

    内存分配,主要指的是在堆上的分配, 一般的,对象的内存分配都是在堆上进行,但现代技术也支持将对象拆成标量类型(标量类型即原子类型,表示单个值,可以是基本类型或String等),然后在栈上分配,在栈上分 ...

  4. 用docker-compose部署postgres+ postgis

    20190411更新.之前写的太啰嗦,也不删了,重新来.小坑还是有的 psql 命令行客户端 因为postgres用docker镜像安装,所以host不需要安装pg,只需要安装客户端 sudo apt ...

  5. 【SQL Server备份恢复】维护计划实现备份:每周数据库完整备份、每天差异备份、每小时日志备份

    在数据库管理中,数据库备份是非常重要的. 通过维护计划向导,可以很方便的完成数据库备份. 下面的例子说明了如何实现数据库的备份,具体的备份策略是:每周日一次完整备份.每天差异备份(除周日外).每小时日 ...

  6. nodejs基础(三)

    apache是web服务器,tomcat是应用(java)服务器 ###  开源中国  查找http中加载不同类型文件所需要的Content-type:http://tool.oschina.net/ ...

  7. pyqt小例子

    from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QApplication, QMainWindow imp ...

  8. Xgboost: 一把屠龙刀的自我修养

    目录 引言 Xgboost 参考文献 引言 集成学习, 在机器学习中是一个非常重要的思想: 把多个弱分类器精巧地组合在一起,成为一个很强大的学习器. 集成学习也因此一直处在风口浪边. 集成学习主要分为 ...

  9. nginx中try_files

    location / { try_files $uri $uri/ /index.php?$query_string; } 当用户请求 http://localhost/example 时,这里的 $ ...

  10. easyui 如何为标签动态追加属性实现渲染效果

    简述一下在项目遇到的问题,这边有一个需求,选择不同类型,加载不同的div标签(其中属性是否必填是区分类型的关键) html界面是这样的 <div class="grid_1 lbl&q ...