Skip to content

Add solution and test-cases for problem 1283 #1114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [1283.Find the Smallest Divisor Given a Threshold][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
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`.

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`).

The test cases are generated so that there will be an answer.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: nums = [1,2,5,9], threshold = 6
Output: 5
Explanation: We can get a sum to 17 (1+2+5+9) if the divisor is 1.
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).
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Find the Smallest Divisor Given a Threshold
```go
```

Input: nums = [44,22,33,11,1], threshold = 5
Output: 44
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
package Solution

func Solution(x bool) bool {
return x
import (
"slices"
"sort"
)

func Solution(nums []int, threshold int) int {
m := slices.Max(nums)
var ok func(int) int
ok = func(divisor int) int {
sum := 0
for _, n := range nums {
a := n / divisor
if n%divisor != 0 {
a++
}
sum += a
}
return sum
}
idx := sort.Search(m+1, func(n int) bool {
if n == 0 {
return false
}
return ok(n) <= threshold
})
return idx
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
inputs []int
threshold int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{1, 2, 5, 9}, 6, 5},
{"TestCase2", []int{44, 22, 33, 11, 1}, 5, 44},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.threshold)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.inputs, c.threshold)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading