Skip to content

Commit 4aefdae

Browse files
authored
Merge pull request #1280 from 0xff-dev/3479
Add solution and test-cases for problem 3479
2 parents 174f622 + 7792515 commit 4aefdae

File tree

3 files changed

+120
-26
lines changed

3 files changed

+120
-26
lines changed

leetcode/3401-3500/3479.Fruits-Into-Baskets-III/README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
# [3479.Fruits Into Baskets III][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 III
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: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "math"
4+
5+
const (
6+
INT_MIN = math.MinInt32
7+
)
8+
9+
type SegTree struct {
10+
segNode []int
11+
baskets []int
12+
}
13+
14+
func (this *SegTree) build(p, l, r int) {
15+
if l == r {
16+
this.segNode[p] = this.baskets[l]
17+
return
18+
}
19+
mid := (l + r) >> 1
20+
this.build(p<<1, l, mid)
21+
this.build(p<<1|1, mid+1, r)
22+
this.segNode[p] = max(this.segNode[p<<1], this.segNode[p<<1|1])
23+
}
24+
25+
func (this *SegTree) query(p, l, r, ql, qr int) int {
26+
if ql > r || qr < l {
27+
return INT_MIN
28+
}
29+
if ql <= l && r <= qr {
30+
return this.segNode[p]
31+
}
32+
mid := (l + r) >> 1
33+
return max(this.query(p<<1, l, mid, ql, qr),
34+
this.query(p<<1|1, mid+1, r, ql, qr))
35+
}
36+
37+
func (this *SegTree) update(p, l, r, pos, val int) {
38+
if l == r {
39+
this.segNode[p] = val
40+
return
41+
}
42+
mid := (l + r) >> 1
43+
if pos <= mid {
44+
this.update(p<<1, l, mid, pos, val)
45+
} else {
46+
this.update(p<<1|1, mid+1, r, pos, val)
47+
}
48+
this.segNode[p] = max(this.segNode[p<<1], this.segNode[p<<1|1])
49+
}
50+
51+
func Solution(fruits []int, baskets []int) int {
52+
m := len(baskets)
53+
if m == 0 {
54+
return len(fruits)
55+
}
56+
57+
tree := SegTree{
58+
segNode: make([]int, 4*m+7),
59+
baskets: baskets,
60+
}
61+
tree.build(1, 0, m-1)
62+
63+
count := 0
64+
for i := 0; i < len(fruits); i++ {
65+
l, r, res := 0, m-1, -1
66+
for l <= r {
67+
mid := (l + r) >> 1
68+
if tree.query(1, 0, m-1, 0, mid) >= fruits[i] {
69+
res = mid
70+
r = mid - 1
71+
} else {
72+
l = mid + 1
73+
}
74+
}
75+
if res != -1 && tree.baskets[res] >= fruits[i] {
76+
tree.update(1, 0, m-1, res, INT_MIN)
77+
} else {
78+
count++
79+
}
80+
}
81+
82+
return count
583
}

leetcode/3401-3500/3479.Fruits-Into-Baskets-III/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)