Skip to content

Commit e15daaf

Browse files
add 1362
1 parent 5891ab7 commit e15daaf

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1362|[Closest Divisors](https://leetcode.com/problems/closest-divisors/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1362.java) | |Medium||Math
1112
|1361|[Validate Binary Tree Nodes](https://leetcode.com/problems/validate-binary-tree-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1361.java) | |Medium||Graph
1213
|1360|[Number of Days Between Two Dates](https://leetcode.com/problems/number-of-days-between-two-dates/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1360.java) | |Easy||
1314
|1358|[Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1358.java) | |Medium|String|
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1362. Closest Divisors
5+
*
6+
* Given an integer num, find the closest two integers in absolute difference whose product equals num + 1 or num + 2.
7+
* Return the two integers in any order.
8+
*
9+
* Example 1:
10+
* Input: num = 8
11+
* Output: [3,3]
12+
* 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.
13+
*
14+
* Example 2:
15+
* Input: num = 123
16+
* Output: [5,25]
17+
*
18+
* Example 3:
19+
* Input: num = 999
20+
* Output: [40,25]
21+
*
22+
* Constraints:
23+
* 1 <= num <= 10^9
24+
* */
25+
public class _1362 {
26+
public static class Solution1 {
27+
public int[] closestDivisors(int num) {
28+
int sqrt = (int) Math.sqrt(num);
29+
int left = sqrt + 1;
30+
int right = sqrt + 1;
31+
long product = left * right;
32+
while (product != (long) (num + 1) && product != (long) (num + 2)) {
33+
if (product < (num + 1)) {
34+
left++;
35+
} else if (product > (num + 2)) {
36+
right--;
37+
}
38+
product = left * right;
39+
}
40+
return new int[]{left, right};
41+
}
42+
}
43+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions._1362;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertArrayEquals;
9+
10+
public class _1362Test {
11+
private static _1362.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1362.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
CommonUtils.printArray(solution1.closestDivisors(8));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
CommonUtils.printArray(solution1.closestDivisors(123));
26+
}
27+
28+
@Test
29+
public void test3() {
30+
CommonUtils.printArray(solution1.closestDivisors(999));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)