diff --git a/leetcode/1301-1400/1306.Jump-Game-III/README.md b/leetcode/1301-1400/1306.Jump-Game-III/README.md index 190382a89..850a1c2b0 100644 --- a/leetcode/1301-1400/1306.Jump-Game-III/README.md +++ b/leetcode/1301-1400/1306.Jump-Game-III/README.md @@ -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. +``` ## 结语 diff --git a/leetcode/1301-1400/1306.Jump-Game-III/Solution.go b/leetcode/1301-1400/1306.Jump-Game-III/Solution.go index d115ccf5e..f2fd91a59 100644 --- a/leetcode/1301-1400/1306.Jump-Game-III/Solution.go +++ b/leetcode/1301-1400/1306.Jump-Game-III/Solution.go @@ -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 } diff --git a/leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go b/leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go index 14ff50eb4..555e5a309 100644 --- a/leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go +++ b/leetcode/1301-1400/1306.Jump-Game-III/Solution_test.go @@ -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() { }