Jump game ii, the title guarantees that you can jump to the last position.
Input:2.3.1.1.4] output:2Explanation: The minimum number of jumps to the last position is2. From the subscript for0Skip to subscript1The position of jump1Step, then jump3Step to the last position in the array.Copy the code
- The point 1
Define two greedy steps. One is next_step, which represents the maximum distance that you can jump to each time. Curr_step, when curr_step is reached, a jump must be made. 2. Why does point 2 end in the penultimate step
public class JumpMent {
public static void main(String[] args) {
int[] jumpp = new int[] {3.5.1.1.1.1.4.1.1.1.1};
jump(jumpp);
}
public static int jump(int[] nums) {
int next_steps = 0;
int curr_steps = 0;
int step = 0;
for(int i = 0 ; i < nums.length-1 ; i++) {
System.out.println(i);
if(nums[i] + i > next_steps) {
next_steps = nums[i] + i;
}
if(i == curr_steps) { step++; curr_steps = next_steps; }}returnstep; }}Copy the code