-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGame2.java
More file actions
31 lines (27 loc) · 885 Bytes
/
JumpGame2.java
File metadata and controls
31 lines (27 loc) · 885 Bytes
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
import java.util.Arrays;
public class JumpGame2 {
public static int minJumpRec(int a[], int i) {
if(i == a.length-1) return 0;
int res = Integer.MAX_VALUE;
for(int j=i+1;j<= (a[i]+i);j++) {
int jump = minJumpRec(a,j);
if(jump != Integer.MAX_VALUE) res = Math.min(res, 1+jump);
}
return res;
}
public static int jumpGame(int a[]) {
int dp[] = new int[a.length];
for(int i=1;i<dp.length;i++) dp[i] = Integer.MAX_VALUE;
for(int i=1;i<a.length;i++) {
for(int j=0;j<i;j++) {
if(a[j]+j >= i && dp[j]+1 < dp[i]) dp[i] = 1+dp[j];
}
}
//System.out.println(Arrays.toString(dp));
return dp[a.length-1];
}
public static void main(String args[]) {
int a[] = {2,2};
System.out.println(jumpGame(a));
}
}