Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ba7dd5

Browse files
committed
Merge pull request #387 from 0xff-dev/2279
Add solution and test-cases for problem 2279
2 parents 35a05e0 + b1cd008 commit 1ba7dd5

File tree

3 files changed

+54
-26
lines changed

3 files changed

+54
-26
lines changed

‎leetcode/2201-2300/2279.Maximum-Bags-With-Full-Capacity-of-Rocks/README.md‎

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [2279.Maximum Bags With Full Capacity of Rocks][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 have `n` bags numbered from `0` to `n - 1`. You are given two **0-indexed** integer arrays `capacity` and `rocks`. The i<sup>th</sup> bag can hold a maximum of `capacity[i]` rocks and currently contains `rocks[i]` rocks. You are also given an integer `additionalRocks`, the number of additional rocks you can place in **any** of the bags.
5+
6+
Return the **maximum** number of bags that could have full capacity after placing the additional rocks in some bags.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: capacity = [2,3,4,5], rocks = [1,2,4,4], additionalRocks = 2
12+
Output: 3
13+
Explanation:
14+
Place 1 rock in bag 0 and 1 rock in bag 1.
15+
The number of rocks in each bag are now [2,3,4,4].
16+
Bags 0, 1, and 2 have full capacity.
17+
There are 3 bags at full capacity, so we return 3.
18+
It can be shown that it is not possible to have more than 3 bags at full capacity.
19+
Note that there may be other ways of placing the rocks that result in an answer of 3.
1320
```
1421

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

20-
### 思路1
21-
> ...
22-
Maximum Bags With Full Capacity of Rocks
23-
```go
2424
```
25-
25+
Input: capacity = [10,2,2], rocks = [2,2,0], additionalRocks = 100
26+
Output: 3
27+
Explanation:
28+
Place 8 rocks in bag 0 and 2 rocks in bag 2.
29+
The number of rocks in each bag are now [10,2,2].
30+
Bags 0, 1, and 2 have full capacity.
31+
There are 3 bags at full capacity, so we return 3.
32+
It can be shown that it is not possible to have more than 3 bags at full capacity.
33+
Note that we did not use all of the additional rocks.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(capacity []int, rocks []int, additionalRocks int) int {
6+
diff := make([]int, len(capacity))
7+
for i := 0; i < len(capacity); i++ {
8+
diff[i] = capacity[i] - rocks[i]
9+
}
10+
sort.Ints(diff)
11+
i := 0
12+
ans := 0
13+
for ; i < len(capacity) && additionalRocks > 0; i++ {
14+
if diff[i] <= 0 {
15+
ans++
16+
continue
17+
}
18+
if diff[i] <= additionalRocks {
19+
ans++
20+
additionalRocks -= diff[i]
21+
}
22+
}
23+
return ans
524
}

‎leetcode/2201-2300/2279.Maximum-Bags-With-Full-Capacity-of-Rocks/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+
a, b []int
14+
c int
15+
expect int
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{2, 3, 4, 5}, []int{1, 2, 4, 4}, 2, 3},
18+
{"TestCase2", []int{10, 2, 2}, []int{2, 2, 0}, 100, 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.a, c.b, c.c)
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 %v",
27+
c.expect, got, c.a, c.b, c.c)
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)

AltStyle によって変換されたページ (->オリジナル) /