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 ...
随机推荐
- JavaScript(数组、Date、正则)
数组 创建数组 // 一.自变量创建数组 // 1-通过字面量创建一个空数组 var arr1 = []; console.log(arr1) console.log(typeof arr1); // ...
- LINQ之路14:LINQ Operators之排序和分组(Ordering and Grouping)
本篇继续LINQ Operators的介绍,这里要讨论的是LINQ中的排序和分组功能.LINQ的排序操作符有:OrderBy, OrderByDescending, ThenBy, 和ThenByDe ...
- 在 Linux 上使用 VirtualBox 的命令行管理界面
VirtualBox 拥有一套命令行工具,你可以使用 VirtualBox 的命令行界面 (CLI) 对远程无界面的服务器上的虚拟机进行管理操作.在这篇教程中,你将会学到如何在没有 GUI 的情况下使 ...
- Codeforces 40 E. Number Table
题目链接:http://codeforces.com/problemset/problem/40/E 妙啊... 因为已经确定的格子数目严格小于了$max(n,m)$,所以至少有一行或者一列是空着的, ...
- Anaconda 创建环境
2019-03-25 17:10:51 Anaconda 给不同的项目创建不同的环境真的非常重要,最近在使用flask的时候在base环境中安装flask-bootstrap,竟然将我原本的py3.7 ...
- LOG4NET用法(个人比较喜欢的用法)
LOG4NET用法(个人比较喜欢的用法) http://fanrsh.cnblogs.com/archive/2006/06/08/420546.html
- 深入理解Plasma(二)Plasma 细节
这一系列文章将围绕以太坊的二层扩容框架,介绍其基本运行原理,具体操作细节,安全性讨论以及未来研究方向等.本篇文章主要对 Plasma 一些关键操作的细节进行剖析. 在上一篇文章中我们已经理解了什么是 ...
- python命名空间与作用域
python命名空间与作用域 命名空间是名称与对象之间的关系,可以将命名空间看做是字典,其中的键是名称,值是对象. 命名空间不共享名称. 在命名空间中的名称能将任何python对象作为值,在不同的 ...
- LeetCode Rotatelmage
---恢复内容开始--- You are given an n x n 2D matrix representing an image. Ratate the image by 90 degrees( ...
- python全栈开发笔记---------字符串格式化
字符串格式化 %s 可以接收任何值, %d只能接收整形 .其他类型报错 msg ='i am %s my body' %'ales' print(msg) #i am ales my body msg ...