forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closest-divisors.cpp
41 lines (38 loc) · 1.03 KB
/
closest-divisors.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Time: O(sqrt(n))
// Space: O(1)
class Solution {
public:
vector<int> closestDivisors(int num) {
vector<vector<int>> results = {divisors(num + 1), divisors(num + 2)};
return *min_element(cbegin(results), cend(results),
[](const auto& a, const auto& b) {
return a[1] - a[0] < b[1] - b[0];
});
}
private:
vector<int> divisors(int n) {
for (int d = sqrt(n); d >= 1; --d) {
if (n % d == 0) {
return {d, n / d};
}
}
return {1, n};
}
};
// Time: O(sqrt(n))
// Space: O(1)
class Solution2 {
public:
vector<int> closestDivisors(int num) {
vector<int> result = {1, num};
for (int d = 1; d * d <= num + 2; ++d) {
if ((num + 2) % d == 0) {
result = {d, (num + 2) / d};
}
if ((num + 1) % d == 0) {
result = {d, (num + 1) / d};
}
}
return result;
}
};