-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path45. Jump Game II
More file actions
39 lines (32 loc) · 892 Bytes
/
45. Jump Game II
File metadata and controls
39 lines (32 loc) · 892 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
class Solution {
public int jump(int[] nums) {
int n =nums.length;
int[] dp = new int[n];
Arrays.fill(dp,Integer.MAX_VALUE);
dp[n-1]=0;
for(int i=n-2;i>=0;i--){
int min = Integer.MAX_VALUE;
for(int j=i+1;j<=Math.min(n-1,i+nums[i]);j++){
min = Math.min(min,dp[j]);
}
if(min!=Integer.MAX_VALUE){
dp[i] = min + 1;
}
}
return dp[0];
}
}
class Solution {
public int jump(int[] nums) {
int begin=0,end=0,farthest=0;
int jump=0;
for(int i=0;i<nums.length-1;i++){
farthest = Math.max(farthest, i + nums[i]);
if(i == end){
jump++;
end=farthest;
}
}
return jump;
}
}