Skip to content

Add solution and test-cases for problem 1306 #1252

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 23 additions & 13 deletions leetcode/1301-1400/1306.Jump-Game-III/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [1306.Jump Game III][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
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.

Notice that you can not jump outside of the array at any time.

**Example 1:**

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

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Jump Game III
```go
```
Input: arr = [4,2,3,0,3,1,2], start = 0
Output: true
Explanation:
One possible way to reach at index 3 with value 0 is:
index 0 -> index 4 -> index 1 -> index 3
```

**Example 3:**

```
Input: arr = [3,0,2,1,2], start = 2
Output: false
Explanation: There is no way to reach at index 1 with value 0.
```

## 结语

Expand Down
25 changes: 23 additions & 2 deletions leetcode/1301-1400/1306.Jump-Game-III/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(arr []int, start int) bool {
l := len(arr)
used := make([]bool, l)
used[start] = true
queue := []int{start}
for len(queue) > 0 {
nq := make([]int, 0)
for _, q := range queue {
if arr[q] == 0 {
return true
}
if left := q - arr[q]; left >= 0 && !used[left] {
nq = append(nq, left)
used[left] = true
}
if right := q + arr[q]; right < l && !used[right] {
nq = append(nq, right)
used[right] = true
}
}
queue = nq
}
return false
}
19 changes: 10 additions & 9 deletions leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs []int
start int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{4, 2, 3, 0, 3, 1, 2}, 5, true},
{"TestCase2", []int{4, 2, 3, 0, 3, 1, 2}, 0, true},
{"TestCase3", []int{3, 0, 2, 1, 2}, 2, false},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.inputs, c.start)
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.inputs, c.start)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading