Skip to content

Commit 33cbf2e

Browse files
committed
Add solution and test-cases for problem 1760
1 parent dbd4dda commit 33cbf2e

File tree

3 files changed

+69
-26
lines changed

3 files changed

+69
-26
lines changed

leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
# [1760.Minimum Limit of Balls in a Bag][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+
You are given an integer array `nums` where the `ith` bag contains `nums[i]` balls. You are also given an integer `maxOperations`.
5+
6+
You can perform the following operation at most `maxOperations` times:
7+
8+
- Take any bag of balls and divide it into two new bags with a **positive** number of balls.
9+
10+
- 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.
11+
12+
Your penalty is the **maximum** number of balls in a bag. You want to **minimize** your penalty after the operations.
13+
14+
Return the minimum possible penalty after performing the operations.
715

816
**Example 1:**
917

1018
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
19+
Input: nums = [9], maxOperations = 2
20+
Output: 3
21+
Explanation:
22+
- Divide the bag with 9 balls into two bags of sizes 6 and 3. [9] -> [6,3].
23+
- Divide the bag with 6 balls into two bags of sizes 3 and 3. [6,3] -> [3,3,3].
24+
The bag with the most number of balls has 3 balls, so your penalty is 3 and you should return 3.
1325
```
1426

15-
## 题意
16-
> ...
17-
18-
## 题解
27+
**Example 2:**
1928

20-
### 思路1
21-
> ...
22-
Minimum Limit of Balls in a Bag
23-
```go
2429
```
25-
30+
Input: nums = [2,4,8,2], maxOperations = 4
31+
Output: 2
32+
Explanation:
33+
- Divide the bag with 8 balls into two bags of sizes 4 and 4. [2,4,8,2] -> [2,4,4,4,2].
34+
- 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].
35+
- 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].
36+
- 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].
37+
The bag with the most number of balls has 2 balls, so your penalty is 2, and you should return 2.
38+
```
2639

2740
## 结语
2841

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int, maxOperations int) int {
4+
var can func(int) bool
5+
can = func(limit int) bool {
6+
op := maxOperations
7+
for _, n := range nums {
8+
if n <= limit {
9+
continue
10+
}
11+
// 9 /3
12+
times := n / limit
13+
if n%limit == 0 {
14+
times--
15+
}
16+
if times > op {
17+
return false
18+
}
19+
op -= times
20+
}
21+
return true
22+
}
23+
left, right := 1, 1000000001
24+
ans := -1
25+
for left < right {
26+
mid := (left + right) / 2
27+
if can(mid) {
28+
right = mid
29+
ans = mid
30+
continue
31+
}
32+
left = mid + 1
33+
}
34+
return ans
535
}

leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/Solution_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
limit int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 4, 8, 1}, 4, 2},
18+
{"TestCase2", []int{9}, 2, 3},
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.limit)
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.limit)
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)