LeetCode 911. Online Election
原题链接在这里:https://leetcode.com/problems/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 <= 50000 <= persons[i] <= persons.lengthtimesis a strictly increasing array with all elements in[0, 10^9].TopVotedCandidate.qis called at most10000times per test case.TopVotedCandidate.q(int t)is always called witht >= times[0].
题解:
Given a list of person vote with time, ask <= given time, who is the lead vote person.
The ask is based on time, find the floor key and get the lead vote.
We could have a structure to store mappings between time and lead vote person.
For each vote, accumulate the votes for each person and update the lead. Then update the lead person at the time.
For the given time, use binary search to find floor key and use this floor key time to get the lead person at that time.
Time Complexity: TopVotedCandidate, O(n). n = persons.length. q, O(logn).
Space: O(n).
AC Java:
class TopVotedCandidate {
HashMap<Integer, Integer> cachedMostVote;
int [] times;
public TopVotedCandidate(int[] persons, int[] times) {
this.cachedMostVote = new HashMap<>();
this.times = times;
HashMap<Integer, Integer> count = new HashMap<>();
int n = persons.length;
int lead = -1;
for(int i = 0; i<n; i++){
count.put(persons[i], count.getOrDefault(persons[i], 0)+1);
if(lead == -1 || count.get(persons[i]) >= count.get(lead)){
lead = persons[i];
}
this.cachedMostVote.put(times[i], lead);
}
}
public int q(int t) {
return cachedMostVote.get(getFloorKey(t));
}
private int getFloorKey(int t){
int l = 0;
int r = times.length-1;
while(l <= r){
int mid = l + (r-l)/2;
if(times[mid] == t){
return times[mid];
}else if(times[mid] < t){
l = mid+1;
}else{
r = mid-1;
}
}
return times[r];
}
}
/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
* int param_1 = obj.q(t);
*/
LeetCode 911. 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 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- redis 学习笔记(6)-cluster集群搭建
上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...
- redis 学习笔记-cluster集群搭建
一.下载最新版redis 编译 目前最新版是3.0.7,下载地址:http://www.redis.io/download 编译很简单,一个make命令即可,不清楚的同学,可参考我之前的笔记: red ...
- Redis操作及集群搭建以及高可用配置
NoSQL - Redis 缓存技术 Redis功能介绍 数据类型丰富 支持持久化 多种内存分配及回收策略 支持弱事务 支持高可用 支持分布式分片集群 企业缓存产品介绍 Memcached: 优点:高 ...
- LeetCode - 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 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 【chromium】cef是如何进行版本控制的?
搜了搜cef相关的文章,内容大多是 如何下载源码,如何编译,还有一些源码剖析,但是很少有人说明对cef进行开发时如何保存修改,使用git进行修改后的版本控制. cef是怎么做的? cef源码分为两个部 ...
- QT之类型转换
Qt在进行数据类型转换时,容易忘记如何使用,或者是早已厌倦了百度QString转QByteArray,QByteArray转char,QString转string....... 现在分享一篇QT数据类 ...
- HashMap源码原理
HashMap源码解析(负载因子,树化策略,内部hash实现,resize策略) 内部属性: 负载因子: final float loadFactor(默认为0.75f) 实际容量: int thre ...
- flyway.setBaselineOnMigrate(true);
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayWrappe ...
- Vue -- 项目报错整理(2):IE报错 - ‘SyntaxError:strict 模式下不允许一个属性有多个定义‘ ,基于vue element-ui页面跳转坑的解决
- HTML 注释 和 实体字符
一.注释 在HTML中还有一种特殊的标签——注释标签.如果需要在HTML文档中添加一些便于阅读和理解但又不需要显示在页面中的注释文字,就需要使用注释标签. 注释内容不会显示在浏览器窗口中,但是作为HT ...
- 英语aeroides海蓝宝石aeroides单词
海蓝宝石(aeroides,Aquamarine)是一种含铍.铝的硅酸盐,与祖母绿.乌兰孖努以及碧玺等统称为彩色宝石.海蓝宝石的颜色为天蓝色至海蓝色或带绿的蓝色,它的颜色形成主要是由于含微量的二价铁离 ...
- 你能想象未来的MES系统是什么样吗?
“智能制造”热潮席卷神州大地,在工业4.0热潮,以及国家大力推进中国制造2025的背景下,建设智能工厂,推进智能制造已成为制造企业共同的目标.作为承上启下的车间级综合信息系统,MES系统得到了制造企业 ...
- windows mysql服务器
安装完mysql服务器后,需要启动服务器, 才可提供数据库存储服务.windows上如何启动和关闭mysql服务器呢? 1. 启动 进入mysql的安装目录,如D:\Program Files\mys ...
- Spark排序方式集锦
一.简介 spark中的排序一般可以使用orderBy或sort算子,可以结合负号.ASC/DESC和col进行简单排序.二次排序等情况 二.代码实现 package big.data.analyse ...