当前位置:网站首页>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]
}
}
边栏推荐
- 【Kotlin学习】类、对象和接口——定义类继承结构
- Database execution error: SQL_ mode only_ full_ group_ by:
- Temper cattle ranking problem
- Run flash demo on ECS
- QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
- Crawler career from scratch (V): detailed explanation of re regular expression
- PolyWorks script development learning notes (4) - data import and alignment using file import
- LeetCode每日一题(931. Minimum Falling Path Sum)
- Arduino handles JSON data, arduinojson assistant
- Quickly use markdown to edit articles
猜你喜欢

Go language - Reflection

Trial of the combination of RDS and crawler

There is no open in default browser option in the right click of the vscade editor
[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
![[CSDN]C1训练题解析_第三部分_JS基础](/img/b2/68d53ad09688f7fc922ac65e104f15.png)
[CSDN]C1训练题解析_第三部分_JS基础

【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字

Jenkins learning (II) -- setting up Chinese

Vscode编辑器右键没有Open In Default Browser选项

Liteide is easy to use

CATIA automation object architecture - detailed explanation of application objects (III) systemservice
随机推荐
Win10安装ELK
Win10 quick screenshot
软件测试工程师是做什么的 通过技术测试软件程序中是否有漏洞
QT sub window is blocked, and the main window cannot be clicked after the sub window pops up
Send mail using WP mail SMTP plug-in
Spark overview
Modify idea code
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
LeetCode每日一题(931. Minimum Falling Path Sum)
【Kotlin学习】类、对象和接口——定义类继承结构
Trial of the combination of RDS and crawler
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
Principles of computer composition - cache, connection mapping, learning experience
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
[untitled] use of cmake
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
Hudi 快速体验使用(含操作详细步骤及截图)
Flink学习笔记(八)多流转换