当前位置:网站首页>365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
2022-07-29 05:25:00 【ShowM3TheCode】
List of articles
Two point search completion Memorial
878. The first N A magic number
Code implementation ( Part depends on the solution )
class Solution {
public:
typedef long long ll;
// greatest common divisor greatest common divisor
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a % b);
}
// least common multiple Minimum common multiple
ll lcm(ll a, ll b) {
return a * b / gcd(a, b);
}
int nthMagicalNumber(int n, int a, int b) {
ll ab = lcm(a, b);
// The result of this question is [1, 2 * 10^9] Within the scope of , Optimize to [min({a,b,c}), 2 * 10^9]
ll l = min(a, b) - 1, r = 4e13 + 1;
while (l+1 != r) {
ll mid = (l + r) >> 1;
// Principle of tolerance and exclusion : Calculation cnt by [1,mid] Ugly number in
ll cnt = mid / a + mid / b - mid / ab;
if (cnt < n) {
l = mid;
} else {
r = mid;
}
}
return r % 1000000007;
}
};
911. Online elections
Code implementation ( Self solution )
class TopVotedCandidate {
private:
vector<int> times;
map<int, int> query;
public:
TopVotedCandidate(vector<int>& persons, vector<int>& times) :
times(times) {
map<int, int> _map;
int maxVotes = 0, selectedPerson = 0;
for (int i = 0; i < times.size(); i++) {
_map[persons[i]]++;
for (auto it = _map.begin(); it != _map.end(); it++) {
if (it->second > maxVotes) {
maxVotes = it->second;
selectedPerson = it->first;
}
}
if (maxVotes == _map[persons[i]]) {
selectedPerson = persons[i];
}
query[times[i]] = selectedPerson;
}
}
int q(int t) {
int l = 0, r = times.size() - 1, m = 0;
while (l < r) {
m = (l + r + 1) >> 1;
if (times[m] <= t) l = m;
else r = m - 1;
}
return query[times[l]];
}
};
/** * Your TopVotedCandidate object will be instantiated and called as such: * TopVotedCandidate* obj = new TopVotedCandidate(persons, times); * int param_1 = obj->q(t); */
边栏推荐
- Webrtc audio anti weak network technology (Part 2)
- 510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
- QML custom tabbar
- ARFoundation从零开始5-AR图像跟踪
- 京东云联合Forrester咨询发布混合云报告 云原生成为驱动产业发展新引擎
- Complete ecological map of R & D Efficiency & selection of Devops tools
- 01-01-osg GL3 环境搭建
- 365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
- QML定制TabBar
- 预约中,2022京东云产业融合新品发布会线上开启
猜你喜欢
法线可视化
How mongodb inserts, deletes and updates documents
阿里云架构师梁旭:MES on 云盒,助力客户快速构建数字工厂
365天挑战LeetCode1000题——Day 042 数组序号转换 + 相对名次 离散化处理
串口通讯部分详解
直播预告:京东云DevOps与JFrog制品库的融合
QT series - Installation
2022年SPSSPRO认证杯数学建模B题第二阶段方案及赛后总结
Best practices of JD cloud Distributed Link Tracking in financial scenarios
GPIO的输入输出详解
随机推荐
JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
Helm chart for Kubernetes
How rimworld uploads creative workshops through steamcmd
OCCT学习001-----简介
C语言文件操作
Do you remember the process analysis and function implementation of post notification?
分配内存:malloc()和free()
Architecture analysis of three-tier project and parameter name injection of construction method
副作用和序列点
【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
GPIO的输入输出详解
CMU15-213 Malloc Lab实验记录
Complete ecological map of R & D Efficiency & selection of Devops tools
Visual Basic .Net 如何获取命令参数
CMU15-213 Shell Lab实验记录
What is_ GLIBCXX_ VISIBILITY(default)
7.3-function-templates
The latest tank battle 2022 - full development notes-3
AI应用第一课:C语言支付宝刷脸登录
串口通讯部分详解