1306. Jump Game III
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: 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.
Constraints:
1 <= arr.length <= 5 * 104
0 <= arr[i] < arr.length
0 <= start < arr.length
Solutions
Solution 1: BFS
We can use BFS to determine whether we can reach the index with a value of $0$.
Define a queue $q$ to store the currently reachable indices. Initially, enqueue the $start$ index.
When the queue is not empty, take out the front index $i$ of the queue. If $arr[i] = 0$, return true
. Otherwise, mark the index $i$ as visited. If $i + arr[i]$ and $i - arr[i]$ are within the array range and have not been visited, enqueue them and continue searching.
Finally, if the queue is empty, it means that we cannot reach the index with a value of $0$, so return false
.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
Python3
class Solution:
def canReach(self, arr: List[int], start: int) -> bool:
q = deque([start])
while q:
i = q.popleft()
if arr[i] == 0:
return True
x = arr[i]
arr[i] = -1
for j in (i + x, i - x):
if 0 <= j < len(arr) and arr[j] >= 0:
q.append(j)
return False
Java
class Solution {
public boolean canReach(int[] arr, int start) {
Deque<Integer> q = new ArrayDeque<>();
q.offer(start);
while (!q.isEmpty()) {
int i = q.poll();
if (arr[i] == 0) {
return true;
}
int x = arr[i];
arr[i] = -1;
for (int j : List.of(i + x, i - x)) {
if (j >= 0 && j < arr.length && arr[j] >= 0) {
q.offer(j);
}
}
}
return false;
}
}
C++
class Solution {
public:
bool canReach(vector<int>& arr, int start) {
queue<int> q{{start}};
while (!q.empty()) {
int i = q.front();
q.pop();
if (arr[i] == 0) {
return true;
}
int x = arr[i];
arr[i] = -1;
for (int j : {i + x, i - x}) {
if (j >= 0 && j < arr.size() && ~arr[j]) {
q.push(j);
}
}
}
return false;
}
};
Go
func canReach(arr []int, start int) bool {
q := []int{start}
for len(q) > 0 {
i := q[0]
q = q[1:]
if arr[i] == 0 {
return true
}
x := arr[i]
arr[i] = -1
for _, j := range []int{i + x, i - x} {
if j >= 0 && j < len(arr) && arr[j] >= 0 {
q = append(q, j)
}
}
}
return false
}
TypeScript
function canReach(arr: number[], start: number): boolean {
const q = [start];
for (const i of q) {
if (arr[i] === 0) {
return true;
}
if (arr[i] === -1 || arr[i] === undefined) {
continue;
}
q.push(i + arr[i], i - arr[i]);
arr[i] = -1;
}
return false;
}