-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJumpGameII.java
34 lines (27 loc) · 1.02 KB
/
JumpGameII.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
27
28
29
30
31
32
33
34
package main.java.topcodingquestion.arraysandstrings;
/*
Initiate the maximum position that one could reach starting from the current index i or before: max_pos = nums[0].
Initiate the maximum position reachable during the current jump: max_steps = nums[0].
Initiate number of steps: at least one, if array has more than 1 cell.
Iterate over number of elements in the input array:
If max_step < i, one needs one more jump: jumps += 1. To minimize the number of jumps, choose the longest possible one: max_steps = max_pos.
Update max_pos = max(max_pos, i + nums[i]).
Return the number of jumps.
*/
public class JumpGameII {
public int jump(int[] nums) {
int n = nums.length;
if (n < 2) return 0;
int maxSteps = nums[0];
int maxPos = nums[0];
int jump = 1;
for (int i = 0; i < n; i++) {
if (maxSteps < i) {
++jump;
maxSteps = maxPos;
}
maxPos = Math.max(maxPos, i + nums[i]);
}
return jump;
}
}