-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
30 lines (29 loc) · 789 Bytes
/
Copy pathSolution.java
File metadata and controls
30 lines (29 loc) · 789 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
public class Solution {
/**
* @param A: A list of lists of integers
* @return: An integer
*/
public int jump(int[] A) {
if (A == null || A.length == 0) {
return -1;
}
int start = 0, end = 0, jumps = 0;
while (end < A.length - 1) {
jumps++;
int farthest = end;
for (int i = start; i <= end; i++) {
if (A[i] + i > farthest) {
farthest = A[i] + i;
}
}
start = end + 1;
end = farthest;
}
return jumps;
}
public static void main(String[] args) {
Solution s = new Solution();
int[] A = new int[] {2, 3, 1, 1, 4};
s.jump(A);
}
}