From 12a2fe995e8f4d49a2abc053736e27c6fa73dd0e Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 5 Aug 2025 08:48:42 +0800 Subject: [PATCH] Add solution and test-cases for problem 3477 --- .../3477.Fruits-Into-Baskets-II/README.md | 43 +++++++++++++------ .../3477.Fruits-Into-Baskets-II/Solution.go | 14 +++++- .../Solution_test.go | 21 +++++---- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/README.md b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/README.md index cc2a2b90d..3c6288a52 100755 --- a/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/README.md +++ b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/README.md @@ -1,28 +1,45 @@ # [3477.Fruits Into Baskets II][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 II -```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/3477.Fruits-Into-Baskets-II/Solution.go b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution.go index d115ccf5e..e9d63386a 100644 --- a/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution.go +++ b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution.go @@ -1,5 +1,15 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(fruits []int, baskets []int) int { + left := len(fruits) + for _, f := range fruits { + for i := range baskets { + if baskets[i] >= f { + left-- + baskets[i] = -1 + break + } + } + } + return left } diff --git a/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution_test.go b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution_test.go index 14ff50eb4..693b11fe4 100644 --- a/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/Solution_test.go +++ b/leetcode/3401-3500/3477.Fruits-Into-Baskets-II/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() { }