From 793b77ce0c65e340baa123052da9cfc3ceda8709 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Thu, 6 Feb 2025 22:25:42 +0800 Subject: [PATCH] Add solution and test-cases for problem 1283 --- .../README.md | 27 +++++++++--------- .../Solution.go | 28 +++++++++++++++++-- .../Solution_test.go | 22 +++++++-------- 3 files changed, 50 insertions(+), 27 deletions(-) diff --git a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/README.md b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/README.md index a287054c5..c571f9732 100644 --- a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/README.md +++ b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution.go b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution.go index d115ccf5e..6cac21188 100644 --- a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution.go +++ b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution.go @@ -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 } diff --git a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution_test.go b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution_test.go index 14ff50eb4..5a03239ea 100644 --- a/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution_test.go +++ b/leetcode/1201-1300/1283.Find-the-Smallest-Divisor-Given-a-Threshold/Solution_test.go @@ -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() { }