Skip to content

Commit 37c6def

Browse files
committed
Create jumpGameII.java
1 parent 5d4f435 commit 37c6def

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

jumpGameII.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//O(n) time complexity
2+
3+
public class Solution {
4+
public int jump(int[] A) {
5+
if(A==null||A.length==0||A.length==1){
6+
return 0;
7+
}
8+
9+
int start = 0;
10+
int end = 0;
11+
int jump_times = 0;
12+
int max = 0;
13+
14+
15+
while(end<A.length) {
16+
max = 0;
17+
jump_times++;
18+
//find the max can be reached by current scope
19+
for(int i=start;i<=end;i++) {
20+
if((A[i]+i)>=(A.length-1)) {
21+
return jump_times;
22+
}
23+
if((A[i]+i)>max) {
24+
max = A[i]+i;
25+
}
26+
}
27+
//update for the next search
28+
start = end + 1;
29+
end = max;
30+
}
31+
return jump_times;
32+
}
33+
}

0 commit comments

Comments
 (0)