当前位置:网站首页>LeetCode每日一题(1362. Closest Divisors)
LeetCode每日一题(1362. Closest Divisors)
2022-07-03 09:01:00 【wangjun861205】
Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.
Return the two integers in any order.
Example 1:
Input: num = 8
Output: [3,3]
Explanation: For num + 1 = 9, the closest divisors are 3 & 3, for num + 2 = 10, the closest divisors are 2 & 5, hence 3 & 3 is chosen.
Example 2:
Input: num = 123
Output: [5,25]
Example 3:
Input: num = 999
Output: [40,25]
Constraints:
- 1 <= num <= 10^9
最优解当然是 n² = num + 1 或者 n² = num + 1, 因为 1 <= num <= 1000000000, 那我们的 n 自然不会大于 40000, 有了这个条件托底, 我们就算用最简单的方法从 40000 一路试到 1 也可以, 当然, 最好是把 n 先判断出来
impl Solution {
pub fn closest_divisors(num: i32) -> Vec<i32> {
if num == 1 {
return vec![1, 2];
}
let num = num as i64;
// 指数
let mut e = 1u32;
loop {
if 2i64.pow(e) >= num {
break;
}
e += 1;
}
// 计算出n的上限
let n = 2i64.pow(e / 2 + e % 2);
let mut a = i64::MAX;
let mut b = 0;
for v in (1..=n).rev() {
if (num + 1) % v == 0 {
if ((num + 1) / v - v).abs() < (a - b).abs() {
a = (num + 1) / v;
b = v;
}
}
if (num + 2) % v == 0 {
if ((num + 2) / v - v).abs() < (a - b).abs() {
a = (num + 2) / v;
b = v;
}
}
}
vec![a as i32, b as i32]
}
}
边栏推荐
- Notes on numerical analysis (II): numerical solution of linear equations
- 【Kotlin学习】类、对象和接口——定义类继承结构
- Construction of simple database learning environment
- Beego learning - Tencent cloud upload pictures
- Liteide is easy to use
- [point cloud processing paper crazy reading classic version 9] - pointwise revolutionary neural networks
- Overview of image restoration methods -- paper notes
- Spark 概述
- LeetCode每日一题(931. Minimum Falling Path Sum)
- Hudi 数据管理和存储概述
猜你喜欢
LeetCode 57. Insert interval
【点云处理之论文狂读经典版11】—— Mining Point Cloud Local Structures by Kernel Correlation and Graph Pooling
【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
[graduation season | advanced technology Er] another graduation season, I change my career as soon as I graduate, from animal science to programmer. Programmers have something to say in 10 years
【点云处理之论文狂读前沿版8】—— Pointview-GCN: 3D Shape Classification With Multi-View Point Clouds
Save the drama shortage, programmers' favorite high-score American drama TOP10
Introduction to the basic application and skills of QT
CSDN markdown editor help document
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
Flink学习笔记(九)状态编程
随机推荐
Go language - JSON processing
Flink学习笔记(九)状态编程
[advanced feature learning on point clouds using multi resolution features and learning]
Hudi 集成 Spark 数据分析示例(含代码流程与测试结果)
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
Redis learning (I)
Computing level network notes
Save the drama shortage, programmers' favorite high-score American drama TOP10
LeetCode 75. Color classification
LeetCode每日一题(1024. Video Stitching)
Spark 集群安装与部署
Beego learning - JWT realizes user login and registration
Serializer rewrite: update and create methods
[point cloud processing paper crazy reading classic version 11] - mining point cloud local structures by kernel correlation and graph pooling
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu
CSDN markdown editor help document
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Recommend a low code open source project of yyds
Idea uses the MVN command to package and report an error, which is not available
【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points