Skip to content

Commit

Permalink
Merge pull request #1054 from 0xff-dev/1760
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1760
  • Loading branch information
6boris authored Dec 24, 2024
2 parents d5cb9b2 + 33cbf2e commit 1bd24b6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 26 deletions.
41 changes: 27 additions & 14 deletions leetcode/1701-1800/1760.Minimum-Limit-of-Balls-in-a-Bag/README.md
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

0 comments on commit 1bd24b6

Please sign in to comment.