diff --git a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/README.md b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/README.md index 74e955fe..349a62d7 100755 --- a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/README.md +++ b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/README.md @@ -1,28 +1,45 @@ # [3479.Fruits Into Baskets III][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 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. + +From left to right, place the fruits according to these rules: + +- 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. +- Each basket can hold **only one** type of fruit. +- If a fruit type **cannot be placed** in any basket, it remains **unplaced**. + +Return the number of fruit types that remain unplaced after all possible allocations are made. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: fruits = [4,2,5], baskets = [3,5,4] -## 题意 -> ... +Output: 1 -## 题解 +Explanation: -### 思路1 -> ... -Fruits Into Baskets III -```go +fruits[0] = 4 is placed in baskets[1] = 5. +fruits[1] = 2 is placed in baskets[0] = 3. +fruits[2] = 5 cannot be placed in baskets[2] = 4. +Since one fruit type remains unplaced, we return 1. ``` +**Example 2:** + +``` +Input: fruits = [3,6,1], baskets = [6,4,7] + +Output: 0 + +Explanation: + +fruits[0] = 3 is placed in baskets[0] = 6. +fruits[1] = 6 cannot be placed in baskets[1] = 4 (insufficient capacity) but can be placed in the next available basket, baskets[2] = 7. +fruits[2] = 1 is placed in baskets[1] = 4. +Since all fruits are successfully placed, we return 0. +``` ## 结语 diff --git a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution.go b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution.go index d115ccf5..d216dd49 100644 --- a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution.go +++ b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution.go @@ -1,5 +1,83 @@ package Solution -func Solution(x bool) bool { - return x +import "math" + +const ( + INT_MIN = math.MinInt32 +) + +type SegTree struct { + segNode []int + baskets []int +} + +func (this *SegTree) build(p, l, r int) { + if l == r { + this.segNode[p] = this.baskets[l] + return + } + mid := (l + r) >> 1 + this.build(p<<1, l, mid) + this.build(p<<1|1, mid+1, r) + this.segNode[p] = max(this.segNode[p<<1], this.segNode[p<<1|1]) +} + +func (this *SegTree) query(p, l, r, ql, qr int) int { + if ql > r || qr < l { + return INT_MIN + } + if ql <= l && r <= qr { + return this.segNode[p] + } + mid := (l + r) >> 1 + return max(this.query(p<<1, l, mid, ql, qr), + this.query(p<<1|1, mid+1, r, ql, qr)) +} + +func (this *SegTree) update(p, l, r, pos, val int) { + if l == r { + this.segNode[p] = val + return + } + mid := (l + r) >> 1 + if pos <= mid { + this.update(p<<1, l, mid, pos, val) + } else { + this.update(p<<1|1, mid+1, r, pos, val) + } + this.segNode[p] = max(this.segNode[p<<1], this.segNode[p<<1|1]) +} + +func Solution(fruits []int, baskets []int) int { + m := len(baskets) + if m == 0 { + return len(fruits) + } + + tree := SegTree{ + segNode: make([]int, 4*m+7), + baskets: baskets, + } + tree.build(1, 0, m-1) + + count := 0 + for i := 0; i < len(fruits); i++ { + l, r, res := 0, m-1, -1 + for l <= r { + mid := (l + r) >> 1 + if tree.query(1, 0, m-1, 0, mid) >= fruits[i] { + res = mid + r = mid - 1 + } else { + l = mid + 1 + } + } + if res != -1 && tree.baskets[res] >= fruits[i] { + tree.update(1, 0, m-1, res, INT_MIN) + } else { + count++ + } + } + + return count } diff --git a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution_test.go b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution_test.go index 14ff50eb..693b11fe 100644 --- a/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution_test.go +++ b/leetcode/3401-3500/3479.Fruits-Into-Baskets-III/Solution_test.go @@ -9,31 +9,30 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + fruits, baskets []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{4, 2, 5}, []int{3, 5, 4}, 1}, + {"TestCase2", []int{3, 6, 1}, []int{6, 4, 7}, 0}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.fruits, c.baskets) 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.fruits, c.baskets) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }