LeetCode - Online Election
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的更多相关文章
- [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 ...
- LeetCode 911. Online Election
原题链接在这里:https://leetcode.com/problems/online-election/ 题目: In an election, the i-th vote was cast fo ...
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- [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 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- [译]ZOOKEEPER RECIPES-Leader Election
选主 使用ZooKeeper选主的一个简单方法是,在创建znode时使用Sequence和Ephemeral标志.主要思想是,使用一个znode,比如"/election",每个客 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- caffe提取每一层中的特征,在matlab或python查看
参考博客: http://blog.csdn.net/abc8730866/article/details/52522843 http://blog.csdn.net/lijiancheng0614/ ...
- java ----> 注解/反射
注解 一个例子,摘自Junit-4.12.jar源码. 1 @Retention(RetentionPolicy.RUNTIME) 2 @Target({java.lang.annotation.El ...
- English trip V2 - A 1. Fastival Teacher:Julia Key:
In this lesson you will learn to talk about dates and times. 课上内容(Lesson) Tell your tutor about a fe ...
- mongodump and mongorestore
mongoexport和mongoimport只能导出/导入某个特定集合 1 mongoexport bin目录下 ./mongoexport <hostname><:port> ...
- javax.net.ssl.SSLException: Certificate doesn't match any of the subject alternative names
问题:在使用 org.apache.http.*下的 CloseableHttpClient 发送https请求时报了以上错误 解决方案一:使用java.net.HttpURLConnection i ...
- MySQL字符串列与整数比较
一.问题说明 为了简便在存储时我们经常将整型字段也以字符串形式存储(如id值),但在筛选比较时就需要将该字段转为数值类型. 二.处理办法 2.1 使用cast函数进行类型转换 cast函数格式---- ...
- ubuntu16.04中设置python3
执行: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo update-alter ...
- 在github上参与开源项目贡献代码
1 登录github, 点击自己感兴趣的repository的fork按钮,这样自己的github主页会有一个拷贝. 2 在自己本地修改同时保持和原来的repository同步: git remote ...
- day38-多进程多线程-进程池
强大的Manage上一篇的数据共享的方式只有两种结构Value和Array.Python中提供了强大的Manage专门用来做数据共享的,其支持的类型非常多,包括,Value, Array,list,d ...
- dede织梦判断导航栏是否有子栏目
以下代码判断导航栏是否含有子栏目 {dede:field name=typeid runphp="yes"} global $dsql; $sql , "; $row = ...