Leetcode Greedy Algorithm Jumping Game (I II) Java

Jumping Game 1:
Title Description
Given an array of non-negative integers, you are initially at the first position of the array.
Each element in the array represents the maximum length you can jump at that position.
Determine whether you can reach the last position.

Example 1:
Input: [2,3,1,1,4]
Output: true
Explanation: We can jump 1 step first, reach position 1 from position 0, and then jump 3 steps from position 1 to the last position.

Idea:
Greedy method: traverse each position in the array in turn, and maintain the farthest reachable position in real time. If the farthest reachable position is greater than or equal to the last position in the array, then the last position can be reached. It returns true directly, if the last position is still unreachable after the traversal, it returns false

Code:

class Solution {
    
    
    public boolean canJump(int[] nums) {
    
    
        int temp = 0;
        for(int i = 0;i<nums.length;i++){
    
    
            if(i <= temp){
    
    
            //注意:如果 i 比 temp 大的话,那说明跳过一个或一个以上的数字,相当于“断了”
                temp = Math.max(temp,i+nums[i]);
                //实时维护可以到达的最远位置
                if(temp >= nums.length-1){
    
    
                    return true;
                }
            }         
        }
        return false;

    }
}

Jumping Game 2:
Title Description
Given an array of non-negative integers, you are initially at the first position of the array. Each element in the array represents the maximum length you can jump at that position. Your goal is to use the least number of jumps to reach the last position of the array.

Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to the last position is 2.
Jump from subscript 0 to the position of subscript 1, skip 1 step, and then skip 3 steps to reach the last position of the array.

Explanation:
Assuming that you can always reach the last position of the array.

Idea:
Assume that the last position of the array can always be reached, not that there is no 0 in the middle, but that there is a route that can cross 0.
Greedy method:
Assume that the array is the
starting position of [2,3,1,1,4,2,1] Is 2, the jumpable range is 3 1. Because 3 can jump farther, so jump to 3, start from 3 again (step++)
Insert picture description here
start from 3, the jumpable range is 1 1 4, because 4 can jump to more Far position, so jump to 4, start again from 4 (step++)
Insert picture description here
use end to indicate the boundary of the range that can be jumped to. In the above example, the first time is orange 1, and the second time is orange 4, traversing to the boundary Update the boundary when

Code:

class Solution {
    
    
    public int jump(int[] nums) {
    
    
        // 假设总是可以达到数组的最后一个位置,不是说中间没有0 ,而是存在可以越过0 的路线    
        int len = nums.length;
        int maxposition = 0;
        int end = 0;
        int step = 0;
        for(int i = 0;i<len-1;i++){
    
    
            //这里注意是遍历到len-1,因为起跳的时候step已经加一了,如果最后一次跳跃到达了最后一个位置,那么遍历到最后一个位置的时候就会再次起跳,所以不能遍历最后一个位置
            maxposition = Math.max(maxposition,i+nums[i]);
            if(i == end){
    
    
                //最开始的时候,i = 0 ,end = 0,step++, 这里可以认为开始起跳必定会落下,因此跳跃次数+1
                //假设nums[0] = 4 0+4=4 即最远位置为4 遍历[1,4]过程中,一直记录着最远位置4,在[1,4]之间的时候,一直记录着能够到达的最远位置,到达边界的时候,将end更新为k
                //注意,[1,4]的最远位置必定不会在[1,4],因为如果在[1,4],表示根本出不了[1,4]这个圈
                end = maxposition;
                step++;
            }
        }
        return step;
    }
}

Guess you like

Origin blog.csdn.net/stonney/article/details/112602470