LeetCode Power Button 55. Jump Game

Title Description (Medium Difficulty)

45 Questions You've already seen this before, but you've just returned to the minimum number of steps from position 0 to the last, and this question is whether or not you can skip back.

leetCode Solution The solutions given in this paper are dynamic programming, which are optimized step by step, but are also slower.However, the idea is still worth referencing, the above is more detailed, here is not verbose.Here, due to the influence of 45 questions, I have rewritten the solution of 45 questions to solve this problem.

The solutions below are all based on 45 Questions What you think, you can go over and look at it, understand it, and then come back to the bottom.

Solution

Code for question 45.

public int jump(int[] nums) {
    int end = 0;
    int maxPosition = 0; 
    int steps = 0;
    for(int i = 0; i < nums.length - 1; i  ){
        //Find the farthest you can jump
        maxPosition = Math.max(maxPosition, nums[i]   i); 
        if( i == end){ //When a boundary is encountered, the boundary is updated and the steps are added by one
            end = maxPosition;
            steps  ;
        }
    }
    return steps;
}

Here, we can completely remove the step and consider whether the currently updated i has exceeded the boundaries.

public boolean canJump(int[] nums) { 
    int end = 0;
    int maxPosition = 0;  
    for(int i = 0; i < nums.length - 1; i  ){
        //The current update exceeds the bounds, which means that 0 appears and returns false directly
        if(end < i){
            return false;
        }
        //Find the farthest you can jump 
        maxPosition = Math.max(maxPosition, nums[i]   i); 
       
        if( i == end){ //When a boundary is encountered, the boundary is updated and the steps are added by one
            end = maxPosition; 
        }
    }
    //Is the farthest distance to the end of the answer
    return maxPosition>=nums.length-1;
} 	

Time complexity: O(n).

Spatial complexity: O (1).

Solution Ershuan Melon

Each time you look for the leftmost subscript that jumps to the current position, the previous code is as follows.

public int jump(int[] nums) {
    int position = nums.length - 1; //Location to find
    int steps = 0;
    while (position != 0) { //Did you reach position 0
        for (int i = 0; i < position; i  ) {
            if (nums[i] >= position - i) {
                position = i; //Update the location you are looking for
                steps  ;
                break;
            }
        }
    }
    return steps;
}

If you modify it here, you just need to decide that you didn't go back to 0 in the end, and if the for loop in while doesn't go into if, it means that if you can't find a location, you have to go back to false.

public boolean canJump(int[] nums) { 
    int position = nums.length - 1; //Location to find
    boolean isUpdate = false; 
    while (position != 0) { //Did you reach position 0
        isUpdate = false;
        for (int i = 0; i < position; i  ) {
            if (nums[i] >= position - i) {
                position = i; //Update the location you are looking for 
                isUpdate = true;
                break;
            }
        }
        //If no if statement enters the for loop, false is returned
        if(!isUpdate){
            return false;
        }
    }
    return true;
}

Time complexity: O (n).

Spatial complexity: O (1).

Solution 3

Let's go straight to the nature of the problem. Unlike 45 questions, we don't need to know the minimum number of steps, so we're not interested in the process of skipping.And if there is no zero inside the array, you can jump from the 0 to the last position anyway.

So we only need to look at the position of 0, if there is 0, we only need to look at the position before 0, can we skip the current 0, if none of the positions before 0 can skip the current 0, then go back to false directly.If you can, look at the 0 at the back.

public boolean canJump(int[] nums) {
    for (int i = 0; i < nums.length - 1; i  ) {
        //Location of 0 found
        if (nums[i] == 0) {
            int j = i - 1;
            boolean isCanSkipZero = false;
            while (j >= 0) {
                //Determine if the element before 0 can skip 0 
                if (j   nums[j] > i) {
                    isCanSkipZero = true;
                    break;
                }
                j--;
            }
            if (!isCanSkipZero) {
                return false;
            }
        }
    }
    return true;
}

However, this time complexity has not been improved, and with @Zhengwen's reminder, you can use the following method.

We don't need to look forward every time to determine if the element in front of 0 can skip over 0. We just need to save the farthest distance we can jump currently with a variable, and then we can judge the farthest distance and the current position of 0.

public boolean canJump(int[] nums) {
    int max = 0;
    for (int i = 0; i < nums.length - 1; i  ) {
        if (nums[i] == 0 && i >= max) {
            return false;
        }
        max = Math.max(max, nums[i]   i);
    }
    return true;
}

Time complexity: O(n).

Spatial complexity: O (1).

Reference resources Here , we don't even need to consider the location of 0, just determine if the maximum distance exceeds the current i.

public boolean canJump(int[] nums) {
    int max = 0; 
    for (int i = 0; i < nums.length; i  ) {
        if (i > max) {
            return false;
        }
        max = Math.max(max, nums[i]   i);
    }
    return true;
}

total

When I finished writing 45 questions, I was confused when I saw Solution. How complex was this question?However, Solution's abstraction of the problem as a dynamic planning idea, as well as the optimization process, is worth learning.

More detailed explanation of popular topics leetcode.wang .

68 original articles published. 8. 20,000 visits+
Private letter follow

Tags: Programming

Posted on Sat, 01 Feb 2020 21:44:40 -0500 by toyartist