当前位置:网站首页>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]
}
}
边栏推荐
- Usage of pandas to obtain MySQL data
- Construction of simple database learning environment
- WARNING: You are using pip version 21.3.1; however, version 22.0.3 is available. Prompt to upgrade pip
- Derivation of Fourier transform
- Vscode Arduino installation Library
- The server denied password root remote connection access
- 【Kotlin学习】运算符重载及其他约定——重载算术运算符、比较运算符、集合与区间的约定
- [CSDN]C1训练题解析_第三部分_JS基础
- LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)
- Desktop icon recognition based on OpenCV
猜你喜欢

Spark cluster installation and deployment

Redis learning (I)
![[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions](/img/a3/b442508af9b059d279cffb34dee9bf.png)
[kotlin learning] control flow of higher-order functions -- lambda return statements and anonymous functions

Solve the problem of disordered code in vscode development, output Chinese and open source code

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

The rise and fall of mobile phones in my perspective these 10 years

Spark overview

Spark 集群安装与部署

Arduino handles JSON data, arduinojson assistant

Hudi学习笔记(三) 核心概念剖析
随机推荐
Apply for domain name binding IP to open port 80 record
QT qstring:: number apply base conversion
Nodemcu-esp8266 development (vscode+platformio+arduino framework): Part 1 -- establishment of engineering template -template
Win10安装ELK
[kotlin learning] classes, objects and interfaces - define class inheritance structure
[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
LeetCode每日一题(1856. Maximum Subarray Min-Product)
Vscode编辑器右键没有Open In Default Browser选项
Usage of pandas to obtain MySQL data
【Kotlin学习】类、对象和接口——定义类继承结构
[solution to the new version of Flink without bat startup file]
Hudi learning notes (III) analysis of core concepts
Utilisation de hudi dans idea
The server denied password root remote connection access
Django operates Excel files through openpyxl to import data into the database in batches.
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
Powerdesign reverse wizard such as SQL and generates name and comment
Computing level network notes
【Kotlin疑惑】在Kotlin类中重载一个算术运算符,并把该运算符声明为扩展函数会发生什么?
Jenkins learning (II) -- setting up Chinese