当前位置:网站首页>Leetcode daily question (1362. closest divisors)
Leetcode daily question (1362. closest divisors)
2022-07-03 09:33: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
Of course, the optimal solution is n² = num + 1 perhaps n² = num + 1, because 1 <= num <= 1000000000, Then our n Naturally, it will not be greater than 40000, With this condition , Even if we use the simplest method from 40000 Try it all the way 1 It's fine too , Of course , It's better to put n Judge first
impl Solution {
pub fn closest_divisors(num: i32) -> Vec<i32> {
if num == 1 {
return vec![1, 2];
}
let num = num as i64;
// Index
let mut e = 1u32;
loop {
if 2i64.pow(e) >= num {
break;
}
e += 1;
}
// To calculate the n Upper limit
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]
}
}
边栏推荐
- Send mail using WP mail SMTP plug-in
- 【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
- Flink学习笔记(八)多流转换
- LeetCode每日一题(2305. Fair Distribution of Cookies)
- 软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
- LeetCode每日一题(1996. The Number of Weak Characters in the Game)
- 图像修复方法研究综述----论文笔记
- Desktop icon recognition based on OpenCV
- About the configuration of vs2008+rade CATIA v5r22
- Flask+supervisor installation realizes background process resident
猜你喜欢
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说

Utilisation de hudi dans idea

npm install安装依赖包报错解决方法

Modify idea code
![[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals](/img/8d/938e232c1016cabe9ee0f72be87a22.png)
[kotlin learning] operator overloading and other conventions -- overloading the conventions of arithmetic operators, comparison operators, sets and intervals

【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion

Beego learning - Tencent cloud upload pictures
![[CSDN]C1训练题解析_第三部分_JS基础](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1训练题解析_第三部分_JS基础

LeetCode每日一题(1162. As Far from Land as Possible)

解决Editor.md上传图片获取不到图片地址问题
随机推荐
Linxu learning (4) -- Yum and apt commands
Django operates Excel files through openpyxl to import data into the database in batches.
Hudi integrated spark data analysis example (including code flow and test results)
LeetCode每日一题(985. Sum of Even Numbers After Queries)
Jestson Nano自定义根文件系统创建(支持NVIDIA图形库的最小根文件系统)
IDEA 中使用 Hudi
2022-2-14 learning xiangniuke project - Session Management
[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)
Overview of image restoration methods -- paper notes
Flink学习笔记(八)多流转换
LeetCode每日一题(1024. Video Stitching)
Jestson nano custom root file system creation (supports the smallest root file system of NVIDIA Graphics Library)
Common formulas of probability theory
Construction and test of TFTP server under unbuntu (Debian)
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
PolyWorks script development learning notes (III) -treeview advanced operation
[kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
Install database -linux-5.7
[CSDN]C1训练题解析_第三部分_JS基础
Spark structured stream writing Hudi practice