Skip to content

Commit 1645f1b

Browse files
committed
Add solution and test-cases for problem 1298
1 parent 31756cd commit 1645f1b

File tree

3 files changed

+70
-27
lines changed

3 files changed

+70
-27
lines changed

leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/README.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# [1298.Maximum Candies You Can Get from Boxes][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 have `n` boxes labeled from 0 to `n - 1`. You are given four arrays: `status`, `candies`, `keys`, and `containedBoxes` where:
5+
6+
- `status[i]` is `1` if the `ith` box is open and `0` if the `ith` box is closed,
7+
- `candies[i]` is the number of candies in the `ith` box,
8+
- `keys[i]` is a list of the labels of the boxes you can open after opening the `ith` box.
9+
- `containedBoxes[i]` is a list of the boxes you found inside the `ith` box.
10+
11+
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.
12+
13+
Return the maximum number of candies you can get following the rules above.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0]
19+
Output: 16
20+
Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2.
21+
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.
22+
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.
23+
Total number of candies collected = 7 + 4 + 5 = 16 candy.
1324
```
1425

15-
## 题意
16-
> ...
26+
**Example 2:**
1727

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Candies You Can Get from Boxes
23-
```go
2428
```
25-
29+
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]
30+
Output: 6
31+
Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys.
32+
The total number of candies will be 6.
33+
```
2634

2735
## 结语
2836

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(status []int, candies []int, keys [][]int, containedBoxes [][]int, initialBoxes []int) int {
4+
queue := make([]int, 0)
5+
toBeUsed := make(map[int]struct{})
6+
for _, b := range initialBoxes {
7+
if status[b] == 1 {
8+
queue = append(queue, b)
9+
continue
10+
}
11+
toBeUsed[b] = struct{}{}
12+
}
13+
14+
ans := 0
15+
for len(queue) > 0 {
16+
nq := make([]int, 0)
17+
for _, b := range queue {
18+
ans += candies[b]
19+
for _, unlock := range keys[b] {
20+
status[unlock] = 1
21+
if _, ok := toBeUsed[unlock]; ok {
22+
nq = append(nq, unlock)
23+
delete(toBeUsed, unlock)
24+
}
25+
}
26+
27+
for _, box := range containedBoxes[b] {
28+
if status[box] == 1 {
29+
nq = append(nq, box)
30+
continue
31+
}
32+
toBeUsed[box] = struct{}{}
33+
}
34+
35+
}
36+
queue = nq
37+
}
38+
return ans
539
}

leetcode/1201-1300/1298.Maximum-Candies-You-Can-Get-from-Boxes/Solution_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
status, candies []int
14+
keys, containedBoxes [][]int
15+
initialBoxes []int
16+
expect int
1517
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
18+
{"TestCase1", []int{1, 0, 1, 0}, []int{7, 5, 4, 100}, [][]int{{}, {}, {1}, {}}, [][]int{{1, 2}, {3}, {}, {}}, []int{0}, 16},
19+
{"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},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.status, c.candies, c.keys, c.containedBoxes, c.initialBoxes)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v %v %v",
28+
c.expect, got, c.status, c.candies, c.keys, c.containedBoxes, c.initialBoxes)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)