#1306

Jump Game III

specialist · 605 · lc medium +29 · verified · 66.7% accepted · 4,332 likes · top 72%

Description

Starting at index start in a non-negative integer array arr, at each index i you may jump forward to i + arr[i] or backward to i - arr[i] (staying within bounds).

Determine whether it is possible to reach any index whose value is 0.

Example 1:

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:

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.

Code

1
2
3