Leetcode 1306) Jump Game III
in Algorithms
class Solution {
public boolean recursive(int ind,int[] arr, boolean[] position){
if(ind>=arr.length || ind<0){
return false;
}
if(arr[ind]==0){
return true;
}
if(position[ind]){
return false;
}
position[ind]=true;
if(recursive(arr[ind]+ind,arr,position) || recursive(-arr[ind]+ind,arr,position)){
return true;
}else{
return false;
}
}
public boolean canReach(int[] arr, int start) {
boolean[] position = new boolean[arr.length];
return recursive(start,arr,position);
}
}
다른 풀이
class Solution {
public boolean canReach(int[] arr, int start) {
if(start>=0 && start<arr.length && arr[start]>=0){
if(arr[start]==0) return true;
arr[start] = arr[start] * -1;
return canReach(arr, start+arr[start]) || canReach(arr, start-arr[start]);
}
return false;
}
}