-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathJumpGame3.java
49 lines (43 loc) · 1.21 KB
/
JumpGame3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*https://leetcode.com/problems/jump-game-iii/*/
class Solution {
boolean[] visited;
public boolean canReach(int[] arr, int start) {
visited = new boolean[arr.length];
return check(arr,start);
}
private boolean check(int[] arr, int index)
{
if (arr[index] == 0) return true;
if (visited[index]) return false;
visited[index] = true;
boolean result = false;
if (index+arr[index] < arr.length) result |= check(arr,index+arr[index]);
if (index-arr[index] >= 0) result |= check(arr,index-arr[index]);
visited[index] = false;
return result;
}
}
class Solution {
int[] arr;
int start;
boolean[] dp;
public boolean canReach(int[] arr, int start) {
this.arr=arr;
this.start=start;
dp=new boolean[arr.length];
return dfs(start);
}
public boolean dfs(int index){
if(index<0 || index>=arr.length){
return false;
}
else if(dp[index]){
return false;
}
if(arr[index]==0){
return true;
}
dp[index]=true;
return dfs(index+arr[index]) || dfs(index-arr[index]);
}
}