From 1645f1b20dbb815c9f2f272ca96b96e394f4694b Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 3 Jun 2025 09:25:36 +0800 Subject: [PATCH] Add solution and test-cases for problem 1298 --- .../README.md | 36 +++++++++++------- .../Solution.go | 38 ++++++++++++++++++- .../Solution_test.go | 23 +++++------ 3 files changed, 70 insertions(+), 27 deletions(-) diff --git a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/README.md b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/README.md index 7da91a6db..302daa1a8 100644 --- a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/README.md +++ b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/README.md @@ -1,28 +1,36 @@ # [1298.Maximum Candies You Can Get from Boxes][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 have `n` boxes labeled from 0 to `n - 1`. You are given four arrays: `status`, `candies`, `keys`, and `containedBoxes` where: + +- `status[i]` is `1` if the `ith` box is open and `0` if the `ith` box is closed, +- `candies[i]` is the number of candies in the `ith` box, +- `keys[i]` is a list of the labels of the boxes you can open after opening the `ith` box. +- `containedBoxes[i]` is a list of the boxes you found inside the `ith` box. + +You are given an integer array `initialBoxes` that contains the labels of the boxes you initially have. You can take all the candies in **any open box** and you can use the keys in it to open new boxes and you also can use the boxes you find in it. + +Return the maximum number of candies you can get following the rules above. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] +Output: 16 +Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. +Box 1 is closed and you do not have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. +In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. +Total number of candies collected = 7 + 4 + 5 = 16 candy. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Maximum Candies You Can Get from Boxes -```go ``` - +Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] +Output: 6 +Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. +The total number of candies will be 6. +``` ## 结语 diff --git a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution.go b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution.go index d115ccf5e..2da780118 100644 --- a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution.go +++ b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution.go @@ -1,5 +1,39 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(status []int, candies []int, keys [][]int, containedBoxes [][]int, initialBoxes []int) int { + queue := make([]int, 0) + toBeUsed := make(map[int]struct{}) + for _, b := range initialBoxes { + if status[b] == 1 { + queue = append(queue, b) + continue + } + toBeUsed[b] = struct{}{} + } + + ans := 0 + for len(queue) > 0 { + nq := make([]int, 0) + for _, b := range queue { + ans += candies[b] + for _, unlock := range keys[b] { + status[unlock] = 1 + if _, ok := toBeUsed[unlock]; ok { + nq = append(nq, unlock) + delete(toBeUsed, unlock) + } + } + + for _, box := range containedBoxes[b] { + if status[box] == 1 { + nq = append(nq, box) + continue + } + toBeUsed[box] = struct{}{} + } + + } + queue = nq + } + return ans } diff --git a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution_test.go b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution_test.go index 14ff50eb4..320ef0121 100644 --- a/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution_test.go +++ b/leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution_test.go @@ -9,31 +9,32 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + status, candies []int + keys, containedBoxes [][]int + initialBoxes []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 0, 1, 0}, []int{7, 5, 4, 100}, [][]int{{}, {}, {1}, {}}, [][]int{{1, 2}, {3}, {}, {}}, []int{0}, 16}, + {"TestCase2", []int{1, 0, 0, 0, 0, 0}, []int{1, 1, 1, 1, 1, 1}, [][]int{{1, 2, 3, 4, 5}, {}, {}, {}, {}, {}}, [][]int{{1, 2, 3, 4, 5}, {}, {}, {}, {}, {}}, []int{0}, 6}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.status, c.candies, c.keys, c.containedBoxes, c.initialBoxes) 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 %v %v %v", + c.expect, got, c.status, c.candies, c.keys, c.containedBoxes, c.initialBoxes) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }