当前位置:网站首页>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]
}
}
边栏推荐
- Failed building wheel for argon2 cffi when installing Jupiter
- IDEA 中使用 Hudi
- 基于opencv实现桌面图标识别
- [solution to the new version of Flink without bat startup file]
- Win10安装ELK
- Win10 install elk
- Hudi 数据管理和存储概述
- Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 2 --blinker_ Hello_ WiFi (lighting technology - Mobile App control routine)
- Basic knowledge of database design
- 解决Editor.md上传图片获取不到图片地址问题
猜你喜欢
2022-2-14 learning xiangniuke project - Session Management
NPM install installation dependency package error reporting solution
【Kotlin学习】类、对象和接口——带非默认构造方法或属性的类、数据类和类委托、object关键字
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
About the configuration of vs2008+rade CATIA v5r22
小王叔叔的博客目录【持续更新中】
Principles of computer composition - cache, connection mapping, learning experience
数字身份验证服务商ADVANCE.AI顺利加入深跨协 推进跨境电商行业可持续性发展
随机推荐
Arduino handles JSON data, arduinojson assistant
Simple use of MATLAB
基于opencv实现桌面图标识别
Hudi 快速体验使用(含操作详细步骤及截图)
[point cloud processing paper crazy reading frontier edition 13] - gapnet: graph attention based point neural network for exploring local feature
Modify idea code
Overview of database system
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
LeetCode每日一题(1856. Maximum Subarray Min-Product)
Derivation of Fourier transform
WARNING: You are using pip ; however. Later, upgrade PIP failed, modulenotfounderror: no module named 'pip‘
About the configuration of vs2008+rade CATIA v5r22
Word segmentation in full-text indexing
Run flash demo on ECS
Jetson nano custom boot icon kernel logo CBOOT logo
Win10 install elk
[untitled] use of cmake
Installation and uninstallation of pyenv
LeetCode每日一题(1024. Video Stitching)
LeetCode每日一题(1162. As Far from Land as Possible)