Skip to content

Commit d9a3a2e

Browse files
authored
Merge pull request #1279 from 0xff-dev/3477
Add solution and test-cases for problem 3477
2 parents df4288a + 12a2fe9 commit d9a3a2e

File tree

3 files changed

+52
-26
lines changed

3 files changed

+52
-26
lines changed

leetcode/3401-3500/3477.Fruits-Into-Baskets-II/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3477.Fruits Into Baskets II][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 two arrays of integers, `fruits` and `baskets`, each of length n, where `fruits[i]` represents the **quantity** of the `ith` type of fruit, and `baskets[j]` represents the **capacity** of the `jth` basket.
5+
6+
From left to right, place the fruits according to these rules:
7+
8+
- Each fruit type must be placed in the **leftmost available basket** with a capacity **greater than or equal** to the quantity of that fruit type.
9+
- Each basket can hold **only one** type of fruit.
10+
- If a fruit type **cannot be placed** in any basket, it remains **unplaced**.
11+
12+
Return the number of fruit types that remain unplaced after all possible allocations are made.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
17+
Input: fruits = [4,2,5], baskets = [3,5,4]
1418
15-
## 题意
16-
> ...
19+
Output: 1
1720
18-
## 题解
21+
Explanation:
1922
20-
### 思路1
21-
> ...
22-
Fruits Into Baskets II
23-
```go
23+
fruits[0] = 4 is placed in baskets[1] = 5.
24+
fruits[1] = 2 is placed in baskets[0] = 3.
25+
fruits[2] = 5 cannot be placed in baskets[2] = 4.
26+
Since one fruit type remains unplaced, we return 1.
2427
```
2528

29+
**Example 2:**
30+
31+
```
32+
Input: fruits = [3,6,1], baskets = [6,4,7]
33+
34+
Output: 0
35+
36+
Explanation:
37+
38+
fruits[0] = 3 is placed in baskets[0] = 6.
39+
fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7.
40+
fruits[2] = 1 is placed in baskets[1] = 4.
41+
Since all fruits are successfully placed, we return 0.
42+
```
2643

2744
## 结语
2845

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(fruits []int, baskets []int) int {
4+
left := len(fruits)
5+
for _, f := range fruits {
6+
for i := range baskets {
7+
if baskets[i] >= f {
8+
left--
9+
baskets[i] = -1
10+
break
11+
}
12+
}
13+
}
14+
return left
515
}

leetcode/3401-3500/3477.Fruits-Into-Baskets-II/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+
fruits, baskets []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{4, 2, 5}, []int{3, 5, 4}, 1},
17+
{"TestCase2", []int{3, 6, 1}, []int{6, 4, 7}, 0},
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.fruits, c.baskets)
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.fruits, c.baskets)
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)