LeetCode-Greedy-44-M: Jumper

Article Directory


Given a non-negative integer array, you initially located in the first position of the array.
Each element in the array represents the maximum length you can jump in that position.
To determine whether you are able to reach a final position.

Input: [2,3,1,1,4]
Output: true
explanation: we can jump a first step, from the position 0 to the position 1, then jump from step 3 1 reaches the position of the last position.

Thinking

The official solution to a problem
which is a dynamic programming problem is usually solved and understand a dynamic programming problem requires the following four steps:
(1) the use of recursive backtracking to solve the problem
(2) optimization (top-down dynamic programming) using the memory table
(3) removing the recursive portion (bottom-up dynamic programming)
(4) using techniques reduce the time and space complexity

Solution 1- Qing Qi

To go along, if the current position to the end will be able ok, that has been subtly updated farthest point can reach up to the time the current point position.

When execution: 2 ms, beat the 61.87% of all users to submit in Java
memory consumption: 40.6 MB, defeated 31.63% of all users to submit in Java


https://leetcode-cn.com/problems/jump-game/solution/55-by-ikaruga/

class Solution {
    public boolean canJump(int[] nums) {
        int k=0;
        for(int i=0; i<nums.length; i++){
            if(i>k){
                return false;
            }

			if(k >= nums.length){
                return true;
            }
            
            k = Math.max(k, nums[i]+i);
        }
        return true;
    }
}
Published 77 original articles · won praise 16 · views 1870

Guess you like

Origin blog.csdn.net/Xjheroin/article/details/104121198