Skip to content

Commit 793b77c

Browse files
committed
Add solution and test-cases for problem 1283
1 parent d569e0c commit 793b77c

File tree

3 files changed

+50
-27
lines changed

3 files changed

+50
-27
lines changed

leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [1283.Find the Smallest Divisor Given a Threshold][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 array of integers `nums` and an integer `threshold`, we will choose a positive integer `divisor`, divide all the array by it, and sum the division's result. Find the **smallest** `divisor` such that the result mentioned above is less than or equal to `threshold`.
5+
6+
Each result of the division is rounded to the nearest integer greater than or equal to that element. (For example: `7/3 = 3` and `10/2 = 5`).
7+
8+
The test cases are generated so that there will be an answer.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: nums = [1,2,5,9], threshold = 6
14+
Output: 5
15+
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
16+
If the divisor is 4 we can get a sum of 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2).
1317
```
1418

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

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Find the Smallest Divisor Given a Threshold
23-
```go
2421
```
25-
22+
Input: nums = [44,22,33,11,1], threshold = 5
23+
Output: 44
24+
```
2625

2726
## 结语
2827

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

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"slices"
5+
"sort"
6+
)
7+
8+
func Solution(nums []int, threshold int) int {
9+
m := slices.Max(nums)
10+
var ok func(int) int
11+
ok = func(divisor int) int {
12+
sum := 0
13+
for _, n := range nums {
14+
a := n / divisor
15+
if n%divisor != 0 {
16+
a++
17+
}
18+
sum += a
19+
}
20+
return sum
21+
}
22+
idx := sort.Search(m+1, func(n int) bool {
23+
if n == 0 {
24+
return false
25+
}
26+
return ok(n) <= threshold
27+
})
28+
return idx
529
}

leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
inputs []int
14+
threshold int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 5, 9}, 6, 5},
18+
{"TestCase2", []int{44, 22, 33, 11, 1}, 5, 44},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.inputs, c.threshold)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
27+
c.expect, got, c.inputs, c.threshold)
2828
}
2929
})
3030
}
3131
}
3232

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

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

0 commit comments

Comments
 (0)