From 33cbf2ef00c7ad53c037fea0cfb56edc024d2063 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sat, 7 Dec 2024 12:24:47 +0800 Subject: [PATCH] Add solution and test-cases for problem 1760 --- .../README.md | 41 ++++++++++++------- .../Solution.go | 34 ++++++++++++++- .../Solution_test.go | 20 ++++----- 3 files changed, 69 insertions(+), 26 deletions(-) diff --git a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md index d556c9005..7cd8a56d5 100755 --- a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md +++ b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md @@ -1,28 +1,41 @@ # [1760.Minimum Limit of Balls in a Bag][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 +You are given an integer array `nums` where the `ith` bag contains `nums[i]` balls. You are also given an integer `maxOperations`. + +You can perform the following operation at most `maxOperations` times: + +- Take any bag of balls and divide it into two new bags with a **positive** number of balls. + + - For example, a bag of `5` balls can become two new bags of `1` and `4` balls, or two new bags of `2` and `3` balls. + +Your penalty is the **maximum** number of balls in a bag. You want to **minimize** your penalty after the operations. + +Return the minimum possible penalty after performing the operations. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: nums = [9], maxOperations = 2 +Output: 3 +Explanation: +- Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3]. +- Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -> [3,3,3]. +The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Minimum Limit of Balls in a Bag -```go ``` - +Input: nums = [2,4,8,2], maxOperations = 4 +Output: 2 +Explanation: +- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -> [2,4,4,4,2]. +- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,4,4,4,2] -> [2,2,2,4,4,2]. +- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,4,4,2] -> [2,2,2,2,2,4,2]. +- Divide the bag with 4 balls into two bags of sizes 2 and 2. [2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2]. +The bag with the most number of balls has 2 balls, so your penalty is 2, and you should return 2. +``` ## 结语 diff --git a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution.go b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution.go index d115ccf5e..52fbf17d6 100644 --- a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution.go +++ b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution.go @@ -1,5 +1,35 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int, maxOperations int) int { + var can func(int) bool + can = func(limit int) bool { + op := maxOperations + for _, n := range nums { + if n <= limit { + continue + } + // 9 /3 + times := n / limit + if n%limit == 0 { + times-- + } + if times > op { + return false + } + op -= times + } + return true + } + left, right := 1, 1000000001 + ans := -1 + for left < right { + mid := (left + right) / 2 + if can(mid) { + right = mid + ans = mid + continue + } + left = mid + 1 + } + return ans } diff --git a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution_test.go b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution_test.go index 14ff50eb4..b5415335c 100644 --- a/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution_test.go +++ b/leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + limit int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{2, 4, 8, 1}, 4, 2}, + {"TestCase2", []int{9}, 2, 3}, } // 开始测试 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.limit) 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.limit) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }