Skip to content

Commit 7ba49f4

Browse files
committed
Add solution and test-cases for problem 1362
1 parent f0a9eb1 commit 7ba49f4

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

leetcode/1301-1400/1362.Closest-Divisors/README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
# [1362.Closest Divisors][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer `num`, find the closest two integers in absolute difference whose product equals `num + 1` or `num + 2`.
5+
6+
Return the two integers in any order.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: num = 8
12+
Output: [3,3]
13+
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.
1314
```
1415

15-
## 题意
16-
> ...
16+
**Example 2:**
1717

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Closest Divisors
23-
```go
18+
```
19+
Input: num = 123
20+
Output: [5,25]
2421
```
2522

23+
**Example 3:**
24+
25+
```
26+
Input: num = 999
27+
Output: [40,25]
28+
```
2629

2730
## 结语
2831

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
func factor(n int) (int, int) {
6+
sn := int(math.Sqrt(float64(n)))
7+
a, b := 1, n
8+
for i := sn; i >= 1; i-- {
9+
if n%i != 0 {
10+
continue
11+
}
12+
a, b = i, n/i
13+
break
14+
}
15+
return a, b
16+
}
17+
18+
func Solution(num int) []int {
19+
a1, b1 := factor(num + 1)
20+
a2, b2 := factor(num + 2)
21+
diff1 := b1 - a1
22+
if diff1 < 0 {
23+
diff1 = -diff1
24+
}
25+
diff2 := b2 - a2
26+
if diff2 < 0 {
27+
diff2 = -diff2
28+
}
29+
if diff1 < diff2 {
30+
return []int{a1, b1}
31+
}
32+
return []int{a2, b2}
533
}

leetcode/1301-1400/1362.Closest-Divisors/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 8, []int{3, 3}},
17+
{"TestCase2", 123, []int{5, 25}},
18+
{"TestCase3", 999, []int{25, 40}},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)