惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
A
Arctic Wolf
Webroot Blog
Webroot Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Security Latest
Security Latest
H
Heimdal Security Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
T
Tor Project blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
GbyAI
GbyAI
The Last Watchdog
The Last Watchdog
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
A
About on SuperTechFans
M
MIT News - Artificial intelligence
V
V2EX
V
Visual Studio Blog
Recorded Future
Recorded Future
博客园 - 叶小钗
F
Fortinet All Blogs
L
Lohrmann on Cybersecurity
The GitHub Blog
The GitHub Blog
博客园 - Franky
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Secure Thoughts
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
I
InfoQ
SecWiki News
SecWiki News
Blog — PlanetScale
Blog — PlanetScale
Engineering at Meta
Engineering at Meta
J
Java Code Geeks
B
Blog RSS Feed
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
H
Help Net Security

博客园 - Grandyang

[LeetCode] 1372. Longest ZigZag Path in a Binary Tree 二叉树中的最长交错路径 [LeetCode] 1371. Find the Longest Substring Containing Vowels in Even Counts 每个元音包含偶数次的最长子字符串 [LeetCode] 1370. Increasing Decreasing String 上升下降字符串 [LeetCode] 1368. Minimum Cost to Make at Least One Valid Path in a Grid 使网格图至少有一条有效路径的最小代价 [LeetCode] 1367. Linked List in Binary Tree 二叉树中的链表 [LeetCode] 1366. Rank Teams by Votes 通过投票对团队排名 [LeetCode] 1365. How Many Numbers Are Smaller Than the Current Number 有多少小于当前数字的数字 [LeetCode] 1363. Largest Multiple of Three 形成三的最大倍数 [LeetCode] 1361. Validate Binary Tree Nodes 验证二叉树 [LeetCode] 1360. Number of Days Between Two Dates 日期之间隔几天 [LeetCode] 1359. Count All Valid Pickup and Delivery Options 有效的快递序列数目 [LeetCode] 1358. Number of Substrings Containing All Three Characters 包含所有三种字符的子字符串数目 [LeetCode] 1357. Apply Discount Every n Orders 每隔n个顾客打折 [LeetCode] 1356. Sort Integers by The Number of 1 Bits 根据数字二进制下1 的数目排序 [LeetCode] 1354. Construct Target Array With Multiple Sums 多次求和构造目标数组 [LeetCode] 1353. Maximum Number of Events That Can Be Attended 最多可以参加的会议数目 [LeetCode] 1352. Product of the Last K Numbers 最后 K 个数的乘积 [LeetCode] 1351. Count Negative Numbers in a Sorted Matrix 统计有序矩阵中的负数 [LeetCode] 1349. Maximum Students Taking Exam 参加考试的最大学生数
[LeetCode] 1362. Closest Divisors 最接近的因数
Grandyang · 2023-12-21 · via 博客园 - Grandyang

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

这道题说是给了一个数字 num,让找 num+1 或者 num+2 数字的两个因子使得其差的绝对值最小。题意并不难理解,主要就是找给定数字的因子,然后比较其差的绝对值就行了。由于对 num+1 和 num+2 的操作是相同的,所以用一个子函数来实现比较好,找因子并不需要从1遍历到其本身,因为每对儿因子只要处理一次就行了,可以从其平方根遍历到1,对于遍历到的数字i,若其能被 num 整除,则计算两个因子i和 num/i 的差的绝对值,若其小于 diff,则用其来更新 diff,并且将这两个数字记录到 res 中,这样最终结果 res 中保存的就是差的绝对值最小的两个因子了。在主函数中,分别对 num+1 和 num+2 调用这个子函数,然后将得到的两组结果再次分别计算差的绝对值,返回较小的那组即可,参见代码如下:

解法一:

class Solution {
public:
    vector<int> closestDivisors(int num) {
        vector<int> res1 = find(num + 1), res2 = find(num + 2);
        return abs(res1[0] - res1[1]) < abs(res2[0] - res2[1]) ? res1 : res2;
    }
    vector<int> find(int num) {
        vector<int> res;
        int diff = INT_MAX;
        for (int i = sqrt(num); i > 0; --i) {
            if (num % i != 0) continue;
            if (abs(num / i - i) < diff) {
                diff = abs(num / i - i);
                res = {num / i, i};
            }
        }
        return res;
    }
};

其实这道题并不用遍历所有的因子组合,而是从平方根开始往1遍历,第一个找到的因子对儿就一定是差值最小的,为什么呢?因为若一个平方数,其两个平方根作为因子,差的绝对值为0,已经是最小的了,所以偏离平方根的因子的差的绝对值一定是越来越大的,所以找到的第一对儿因子就一定是满足题意的。这里我们从 num+2 的平方根开始往1遍历,对于每个遍历到的数字i,若其是 num+1 的因子,直接返回因子对儿,否则看下若其是 num+2 的因子,直接返回因子对儿即可,参见代码如下:

解法二:

class Solution {
public:
    vector<int> closestDivisors(int num) {
        for (int i = sqrt(num + 2); i > 0; --i) {
            if ((num + 1) % i == 0) {
                return {i, (num + 1) / i};
            }
            if ((num + 2) % i == 0) {
                return {i, (num + 2) / i};
            }
        }
        return {};
    }
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1362

类似题目:

Distinct Prime Factors of Product of Array

参考资料:

https://leetcode.com/problems/closest-divisors

https://leetcode.com/problems/closest-divisors/solutions/517595/java-c-python-easy-and-concise/

LeetCode All in One 题目讲解汇总(持续更新中...)