原题链接在这里: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. 1 <= persons.length = times.length <= 5000
  2. 0 <= persons[i] <= persons.length
  3. times is a strictly increasing array with all elements in [0, 10^9].
  4. TopVotedCandidate.q is called at most 10000 times per test case.
  5. TopVotedCandidate.q(int t) is always called with t >= 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的更多相关文章

  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 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  3. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  4. redis 学习笔记(6)-cluster集群搭建

    上次写redis的学习笔记还是2014年,一转眼已经快2年过去了,在段时间里,redis最大的变化之一就是cluster功能的正式发布,以前要搞redis集群,得借助一致性hash来自己搞shardi ...

  5. redis 学习笔记-cluster集群搭建

    一.下载最新版redis 编译 目前最新版是3.0.7,下载地址:http://www.redis.io/download 编译很简单,一个make命令即可,不清楚的同学,可参考我之前的笔记: red ...

  6. Redis操作及集群搭建以及高可用配置

    NoSQL - Redis 缓存技术 Redis功能介绍 数据类型丰富 支持持久化 多种内存分配及回收策略 支持弱事务 支持高可用 支持分布式分片集群 企业缓存产品介绍 Memcached: 优点:高 ...

  7. 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 ...

  8. 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题是存储的当前累加和的 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. JQuery的使用案例(二级联动,隔行换色,轮播图,广告插入)

    JQuery的使用案例 (一)利用JQuery完成省市二级联动 第一步:确定事件(change事件),在绑定的函数里面获取用户选择的省份 第二步:创建二维数组来存储省份和城市 第三步:遍历二维数组中的 ...

  2. 详细的Hadoop的入门教程-伪分布模式Pseudo-Distributed Operation

    一. 伪分布模式Pseudo-Distributed Operation 这里关于VM虚拟机的安装就不再介绍了,详细请看<VMware虚拟机的三种网络管理模式>一章介绍.这章只介绍hado ...

  3. iOS - 什么!iOS13 又获取不到WiFi了

    iOS 12 适配WiFi 增加隐私权限 https://www.cnblogs.com/baitongtong/p/10179519.html ios13又新增定位权限 别的不说,理解请看上篇文章 ...

  4. 【案例】如何让阀门制造提高排产效率?APS系统帮你实现

    随着公司业务发展,苏州纽威阀门公司将承接来自各个国家的更多产品业务,越来越多的客户要求对产品进行精确的交期预估和管理.而目前对产线的产能管理仅限于人工静态产能计算. 由于产品繁多,生产流程各异,不同产 ...

  5. ip协议栈

    struct iphdr { #if defined(__LITTLE_ENDIAN_BITFIELD) __u8 ihl:4, version:4; #elif defined (__BIG_END ...

  6. 对List集合中的元素进行排序

    原文:http://blog.csdn.net/veryisjava/article/details/51675036 Collections对List集合中的数据进行排序 有时候需要对集合中的元素按 ...

  7. LP线性规划初识

    认识LP 线性规划(Linear Programming) 特指目标函数和约束条件皆为线性的最优化问题. 目标函数: 多个变量形成的函数 约束条件: 由多个等式/不等式形成的约束条件 线性规划: 在线 ...

  8. “GIS DICTIONARY A-Z” 查询页面开发(1)——bs4与词典数据处理

    第一天的工作:找到数据源,数据下载,数据处理. 数据源:"http://webhelp.esri.com/arcgisserver/9.3/java/geodatabases/definit ...

  9. idea操作maven时控制台中文显示乱码/maven项目启动方式

    在idea中通过maven启动项目时,在前台显示数据库信息,没有中文乱码问题,在控制台中mybatis显示数据库的信息,中文显示乱码. 在程序中用 System.out.println 输出中文的时候 ...

  10. 【解决】Pod 状态一直 Terminating,Need to kill Pod

    偶然查看服务时发现有个单副本的 pod 一直处于Terminating状态,已达 8 天之久,且手动删除删除不掉 [appuser@lyj 2019-09-20 14:35:36 ~]$ kubect ...