当前位置:网站首页>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]
}
}
边栏推荐
- Build a solo blog from scratch
- Vscode编辑器右键没有Open In Default Browser选项
- Spark 结构化流写入Hudi 实践
- LeetCode每日一题(931. Minimum Falling Path Sum)
- Tag paste operator (#)
- 【点云处理之论文狂读前沿版12】—— Adaptive Graph Convolution for Point Cloud Analysis
- We have a common name, XX Gong
- Trial of the combination of RDS and crawler
- Recommend a low code open source project of yyds
- Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
猜你喜欢

Temper cattle ranking problem

Hudi学习笔记(三) 核心概念剖析
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion

Beego learning - Tencent cloud upload pictures

2022-2-13 learning the imitation Niuke project - home page of the development community

Spark overview

【点云处理之论文狂读前沿版13】—— GAPNet: Graph Attention based Point Neural Network for Exploiting Local Feature

Recommend a low code open source project of yyds
![[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs](/img/0a/480f1d1eea6f2ecf84fd5aa96bd9fb.png)
[point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs

2022-2-13 learn the imitation Niuke project - Project debugging skills
随机推荐
Run flash demo on ECS
LeetCode每日一题(931. Minimum Falling Path Sum)
Detailed steps of windows installation redis
[kotlin learning] classes, objects and interfaces - classes with non default construction methods or attributes, data classes and class delegates, object keywords
What are the stages of traditional enterprise digital transformation?
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
Spark 结构化流写入Hudi 实践
LeetCode 75. Color classification
[kotlin learning] classes, objects and interfaces - define class inheritance structure
Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
Crawler career from scratch (II): crawl the photos of my little sister ② (the website has been disabled)
ERROR: certificate common name “www.mysql.com” doesn’t match requested host name “137.254.60.11”.
LeetCode 324. Swing sort II
npm install安装依赖包报错解决方法
IDEA 中使用 Hudi
Spark 概述
Database execution error: SQL_ mode only_ full_ group_ by:
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
LeetCode 57. Insert interval
【点云处理之论文狂读经典版9】—— Pointwise Convolutional Neural Networks