当前位置:网站首页>LeetCode每日一题(911. Online Election)
LeetCode每日一题(911. Online Election)
2022-08-07 14:57:00 【wangjun861205】
You are given two integer arrays persons and times. In an election, the ith vote was cast for persons[i] at time times[i].
For each query at a time t, find 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.
Implement the TopVotedCandidate class:
TopVotedCandidate(int[] persons, int[] times) Initializes the object with the persons and times arrays.
int q(int t) Returns the number of the person that was leading the election at time t according to the mentioned rules.
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
TopVotedCandidate topVotedCandidate = new TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]);
topVotedCandidate.q(3); // return 0, At time 3, the votes are [0], and 0 is leading.
topVotedCandidate.q(12); // return 1, At time 12, the votes are [0,1,1], and 1 is leading.
topVotedCandidate.q(25); // return 1, At time 25, the votes are [0,1,1,0,0,1], and 1 is leading (as ties go to the most recent vote.)
topVotedCandidate.q(15); // return 0
topVotedCandidate.q(24); // return 0
topVotedCandidate.q(8); // return 1
Constraints:
- 1 <= persons.length <= 5000
- times.length == persons.length
- 0 <= persons[i] < persons.length
- 0 <= times[i] <= 109
- times is sorted in a strictly increasing order.
- times[0] <= t <= 109
- At most 104 calls will be made to q.
通过记录每个人在每个时间点上获取的票的数量和一个当前最大得票量, 我们可以轻易的得到一个在每个时间点上的得票最多的人, 剩下的就是根据时间 t 进行二分搜索了
use std::collections::HashMap;
struct TopVotedCandidate {
times: Vec<i32>,
leaders: Vec<i32>,
}
impl TopVotedCandidate {
fn new(persons: Vec<i32>, times: Vec<i32>) -> Self {
let mut m = HashMap::new();
let mut max = 0;
let mut leader = 0;
let mut leaders = Vec::new();
for &p in &persons {
*m.entry(p).or_insert(0) += 1;
let v = *m.get(&p).unwrap();
if v >= max {
max = v;
leader = p;
}
leaders.push(leader)
}
Self {
times, leaders }
}
fn q(&self, t: i32) -> i32 {
let mut low = 0usize;
let mut high = self.times.len() - 1;
while low < high {
let mid = (low + high) / 2;
if self.times[mid] < t {
low = mid + 1;
} else {
high = mid;
}
}
if self.times[low] > t {
return self.leaders[low - 1];
}
self.leaders[low]
}
}
边栏推荐
- LeetCode 热题 HOT 100(10.删除链表的倒数第 N 个结点)
- 锁相环工作原理
- 66:第五章:开发admin管理服务:19:开发【查看用户详情,接口】;
- LeetCode 热题 HOT 100(6.正则表达式匹配)
- 想交易场内基金去哪个证券公司开户更快更安全
- Open3D 点云变换(变换矩阵)
- C专家编程 第7章 对内存的思考 7.8 轻松一下---“Thing King”和“页面游戏”
- 小技巧——postman get&&post请求的使用方式
- Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.4 The Pain of Prototypes
- SWM32系列教程7-I2C及其应用
猜你喜欢

LeetCode hot topic HOT 100 (10. Delete the Nth node from the bottom of the linked list)

基于RK3566中RTL8201F网口百兆调试笔记

Nature | 朱轩/陈福和/袁国勇等证实冠状病毒利用宿主半胱天冬酶-6 促进复制

004_Eureka注册中心

ADC外部RC电路电阻和电容选取计算方法

Based on the FPGA VGA display color bar, characters, pictures

002_认识微服务

联盛德W801系列2-WIFI一键配网,信息保存

OpenCV 颜色检测| color detection

触摸屏如何利用无线PPI通信模块远程采集PLC数据?
随机推荐
【Verilog】时序逻辑电路 -- 有限同步状态机[补充]
C专家编程 第8章 为什么程序员无法分清万圣节和圣诞节 8.7 用C语言实现有限状态机
Flink CDC
d浮点小问题
初始百度地图API
005_Ribbon负载均衡
牛客面试高频榜单(第二组)难度:简单&中等
Lianshengde W801 series 2-WIFI one-key distribution network, information preservation
小技巧——postman get&&post请求的使用方式
一天学会从抓包到接口测试,通过智慧物业项目深度解析
【PTA】L2-033 Simple Calculator (25 points)
C专家编程 第7章 对内存的思考 7.8 轻松一下---“Thing King”和“页面游戏”
链路聚合 VRRP
通过代码查看EFCore的SQL语句
实验9 交换网络综合实验
Lianshengde W801 series 1-flash save data routine: save wifi distribution network information
LeetCode 热题 HOT 100(8.三数之和)
(imdb数据集)电影评论分类实战:二分类问题
Programming Experts in C Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.8 Software is more difficult than hardware
锁相环工作原理