Skip to content

Commit 208de90

Browse files
authored
Merge pull request #1252 from 0xff-dev/1306
Add solution and test-cases for problem 1306
2 parents 71f0c65 + b01aa68 commit 208de90

File tree

3 files changed

+56
-24
lines changed

3 files changed

+56
-24
lines changed

leetcode/1301-1400/1306.Jump-Game-III/README.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,38 @@
11
# [1306.Jump Game III][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+
Given an array of non-negative integers `arr`, you are initially positioned at `start` index of the array. When you are at index `i`, you can jump to `i + arr[i]` or `i - arr[i]`, check if you can reach **any** index with value 0.
5+
6+
Notice that you can not jump outside of the array at any time.
77

88
**Example 1:**
99

1010
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
11+
Input: arr = [4,2,3,0,3,1,2], start = 5
12+
Output: true
13+
Explanation:
14+
All possible ways to reach at index 3 with value 0 are:
15+
index 5 -> index 4 -> index 1 -> index 3
16+
index 5 -> index 6 -> index 4 -> index 1 -> index 3
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Jump Game III
23-
```go
21+
```
22+
Input: arr = [4,2,3,0,3,1,2], start = 0
23+
Output: true
24+
Explanation:
25+
One possible way to reach at index 3 with value 0 is:
26+
index 0 -> index 4 -> index 1 -> index 3
2427
```
2528

29+
**Example 3:**
30+
31+
```
32+
Input: arr = [3,0,2,1,2], start = 2
33+
Output: false
34+
Explanation: There is no way to reach at index 1 with value 0.
35+
```
2636

2737
## 结语
2838

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(arr []int, start int) bool {
4+
l := len(arr)
5+
used := make([]bool, l)
6+
used[start] = true
7+
queue := []int{start}
8+
for len(queue) > 0 {
9+
nq := make([]int, 0)
10+
for _, q := range queue {
11+
if arr[q] == 0 {
12+
return true
13+
}
14+
if left := q - arr[q]; left >= 0 && !used[left] {
15+
nq = append(nq, left)
16+
used[left] = true
17+
}
18+
if right := q + arr[q]; right < l && !used[right] {
19+
nq = append(nq, right)
20+
used[right] = true
21+
}
22+
}
23+
queue = nq
24+
}
25+
return false
526
}

leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
14+
start int
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{4, 2, 3, 0, 3, 1, 2}, 5, true},
18+
{"TestCase2", []int{4, 2, 3, 0, 3, 1, 2}, 0, true},
19+
{"TestCase3", []int{3, 0, 2, 1, 2}, 2, false},
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.inputs, c.start)
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",
28+
c.expect, got, c.inputs, c.start)
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)