-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame3.java
More file actions
51 lines (47 loc) · 1.67 KB
/
JumpGame3.java
File metadata and controls
51 lines (47 loc) · 1.67 KB
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
50
51
public class JumpGame3 {
public static boolean jumpGame3(int a[], int i) {
if(i>=a.length || i<0) return false;
else if(a[i] == 0) return true;
else return jumpGame3(a,i+a[i]) || jumpGame3(a, i-a[i]);
}
static int dp[];
public static boolean jumpGame3Dp(int a[], int i) {
if(i>=a.length || i<0) return false;
else if(dp[i] ==1) return false;
else if(a[i] == 0) return true;
else {
boolean t1, t2;
if(i+a[i] < a.length && i+a[i]>= 0 && dp[i] == 1) t1 = false;
else t1 = jumpGame3Dp(a,i+a[i]);
if(i-a[i] < a.length && i-a[i]>= 0 && dp[i] == 1) t2 = false;
else t2 = jumpGame3Dp(a,i-a[i]);
boolean t = t1 || t2;
if(!t) dp[i] = 1;
return t;
}
}
static int dp2[];
static boolean dp3[];
public static boolean jumpGame3Dp2(int a[], int i) {
if(i>=a.length || i<0) return false;
else if(dp2[i] ==1) return dp3[i];
else if(a[i] == 0) return true;
else {
boolean t1 = false, t2 = false;
if(i+a[i] < a.length && i+a[i] >= 0 && a[i+a[i]] == 0) t1 = true;
if(i-a[i] < a.length && i-a[i] >= 0 && a[i-a[i]] == 0) t2 = true;
dp2[i] = 1;
dp3[i] = t1 || t2;
boolean t = jumpGame3Dp2(a,i+a[i]) || jumpGame3Dp2(a,i-a[i]);
return t;
}
}
public static void main(String args[]) {
int a[] = {4,2,3,0,3,1,2};
dp = new int[a.length];
dp2 = new int[a.length];
dp3 = new boolean[a.length];
int start = 5;
System.out.println(jumpGame3Dp2(a,start));
}
}