Skip to content

Commit 3476e56

Browse files
authored
Merge pull request #1407 from 0xff-dev/3074
Add solution and test-cases for problem 3074
2 parents 98e555e + e7a1908 commit 3476e56

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

leetcode/3001-3100/3074.Apple-Redistribution-into-Boxes/README.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
# [3074.Apple Redistribution into Boxes][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 array `apple` of size `n` and an array `capacity` of size `m`.
5+
6+
There are `n` packs where the `ith` pack contains `apple[i]` apples. There are m boxes as well, and the `ith` box has a capacity of `capacity[i]` apples.
7+
8+
Return the **minimum** number of boxes you need to select to redistribute these `n` packs of apples into boxes.
9+
10+
**Note** that, apples from the same pack can be distributed into different boxes.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: apple = [1,3,2], capacity = [4,3,1,5,2]
16+
Output: 2
17+
Explanation: We will use boxes with capacities 4 and 5.
18+
It is possible to distribute the apples as the total capacity is greater than or equal to the total number of apples.
1319
```
1420

15-
## 题意
16-
> ...
21+
**Example 2:**
1722

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Apple Redistribution into Boxes
23-
```go
2423
```
25-
24+
Input: apple = [5,5,5], capacity = [2,4,2,7]
25+
Output: 4
26+
Explanation: We will need to use all the boxes.
27+
```
2628

2729
## 结语
2830

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

3-
func Solution(x bool) bool {
4-
return x
3+
import "sort"
4+
5+
func Solution(apple []int, capacity []int) int {
6+
sum := 0
7+
for _, n := range apple {
8+
sum += n
9+
}
10+
var ret int
11+
sort.Ints(capacity)
12+
for i := len(capacity) - 1; i >= 0 && sum > 0; i-- {
13+
sum -= capacity[i]
14+
ret++
15+
}
16+
return ret
517
}

leetcode/3001-3100/3074.Apple-Redistribution-into-Boxes/Solution_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
apple, capacity []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 3, 2}, []int{4, 3, 1, 5, 2}, 2},
17+
{"TestCase2", []int{5, 5, 5}, []int{2, 4, 2, 7}, 4},
1918
}
2019

2120
// 开始测试
2221
for i, c := range cases {
2322
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
23+
got := Solution(c.apple, c.capacity)
2524
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
26+
c.expect, got, c.apple, c.capacity)
2827
}
2928
})
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)