Jump Game III LeetCode solution in JavaScript

1
// Recursion, Backtracking
2
var canReach = function (arr, start) {
3
if (arr === null || arr.length === 0) {
4
return false;
5
}
6
return dfs(arr, start);
7
// T.C: O(N)
8
// S.C: O(N)
9
};
10

11
const dfs = (arr, idx) => {
12
if (arr[idx] === 0) {
13
return true;
14
}
15
if (idx < 0 || idx > arr.length - 1 || arr[idx] < 0) {
16
return false;
17
}
18
let jump = arr[idx];
19
arr[idx] = -1; // marking this index as visited
20
return dfs(arr, idx - jump) || dfs(arr, idx + jump);
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0