forked from yuduozhou/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJumpGame2.java
26 lines (26 loc) · 852 Bytes
/
JumpGame2.java
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
public class Solution {
public int jump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
if (A.length <= 1) return 0;
int len = A.length;
int steps = 0;
int marker = 0;
// This will lead us to one step from the end.
while (A[marker] < len - 1 - marker){
int maxRange = marker + A[marker];
int maxFrom = marker;
for (int j = 1; j <= A[marker]; j++){
if (j + marker >= len) break;
if (A[marker + j] + marker + j > maxRange){
maxRange = A[marker + j] + marker + j;
maxFrom = marker + j;
}
}
marker = maxFrom;
steps++;
}
// Jump the last step.
return steps + 1;
}
}