forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumpgame2.java
More file actions
executable file
·34 lines (32 loc) · 859 Bytes
/
jumpgame2.java
File metadata and controls
executable file
·34 lines (32 loc) · 859 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
public class Solution {
public int jump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
// Start typing your Java solution below
// DO NOT write main() function
int min = 0;
int max = 0;
int step = 0;
while(max<A.length-1){
int nextmin = max+1;
int nextmax = 0;
for(int i=min;i<=max;i++){
if(nextmax < A[i]+i){
nextmax = A[i]+i;
}
}
if(nextmin<=nextmax){
min = nextmin;
max = nextmax;
step++;
}
else{
break;
}
}
if(max>=A.length-1){
return step;
}
return Integer.MAX_VALUE;
}
}