Runtime: 2 ms, faster than 79.51% of Java online submissions for Jump Game III.
Memory Usage: 52.2 MB, less than 24.27% of Java online submissions for Jump Game III.
class Solution {
int[] visit;
public boolean canReach(int[] arr, int start) {
visit = new int[arr.length];
return canReachRec(arr, start, visit);
}
public boolean canReachRec(int[] arr, int start, int[] visit) {
if(start < 0)
return false;
if(start >= arr.length)
return false;
if(visit[start] != 0)
return false;
if(arr[start] == 0) {
return true;
}
visit[start] = 1;
boolean v1 = canReachRec(arr, start + arr[start], visit);
boolean v2 = canReachRec(arr, start - arr[start], visit);
visit[start] = 0;
return v1 || v2;
}
}