-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJumpGameII.java
More file actions
39 lines (31 loc) · 847 Bytes
/
JumpGameII.java
File metadata and controls
39 lines (31 loc) · 847 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
32
33
34
35
36
37
38
39
package Leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class JumpGameII
{
public static void main( String[] args )
{
JumpGameII obj =new JumpGameII();
System.out.println( obj.jump( new int[] {2,3,1,1,4} ) );
}
public int jump(int[] nums) {
if(nums==null || nums.length==0)
return 0;
int[] jumps= new int[nums.length];
Arrays.fill(jumps, Integer.MAX_VALUE);
jumps[0] = 0;
for(int i=1;i<nums.length;i++)
{
for(int j=0;j<i;j++)
{
if(j+nums[j]>=i)
{
jumps[i] = Math.min(jumps[i],jumps[j]+1);
}
}
}
return jumps[nums.length-1];
}
}