Skip to content

Add solution and test-cases for problem 1298 #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading