Question

Formatted question description: https://leetcode.ca/all/45.html

45	Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

For example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
                Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:
You can assume that you can always reach the last index.

@tag-array
@tag-greedy

Algorithm

The core method is to use Greedy idea of ​​the greedy algorithm to solve it. Think about why? In order to jump to the end quickly, I want to know the range of each step. Greed here is not to choose the position with the farthest jumping power in the range of jumping, because this selection is not necessarily the optimal solution, so to speak it doesn’t feel like a greedy algorithm again.

In fact, what is greedy here is the farthest range that can be reached. It traverses all the positions that can be reached by the current jump, and then predicts the farthest distance that can be jumped in the next step according to the jump force at that position, and greets the farthest range. Once this range reaches the end, the number of steps currently used must be the minimum number of steps.

Two variables cur and pre are required to save the current farthest position that can be reached and the farthest position that can be reached before. As long as cur does not reach the last position, the loop will continue. Pre is first assigned the value of cur, which means the last loop. The farthest position that can be reached later.

  • If the current position i is less than or equal to pre, it means that it is still within the range that the previous jump can reach. The current position is added to the jump force to update cur. The method to update cur is to compare the current cur and i + larger value of A[i],
  • if it is not stated in the question whether the end can be reached, it can also be judged whether pre and cur are equal at this time. If they are equal, it means that cur has not been updated, that is, the end position cannot be reached, return -1.

Code

Java

public class Jump_Game_II {

	class Solution {
        public int jump(int[] nums) {
            if (nums == null || nums.length == 0) {
                return 0;
            }

            int left = 0; // last jump reaching to
            int right = 0; // current jump reaching to

            int count = 0;

            for (int i = 0; i < nums.length - 1; i++) { // @note: i < Len - 1. 也就是停在倒数第二个
                right = Math.max(right, i + nums[i]);
                if (i == left) { // reaching furthest of last jump
                    count++;
                    left = right;
                }
            }

            return count;
        }
    }
}

All Problems

All Solutions