A straightforward dp approach use java, O(n), scan the array backward


  • 0
    M
    public class Solution {
        public boolean canJump(int[] nums) {
            // if(nums.length == 0) return false; 
            if(nums.length == 1) return true;
    
            //The minimum step that the next left element should have
            //usually we need 1 to jump to the next right position
            int minStep = 1; 
    
            for(int i=nums.length-2; i>0; i--) {
                if(nums[i] == 0) {
                    minStep++;
                }
                else {
                    if(nums[i] >= minStep) {
                        minStep = 1;
                    }
                    else minStep++;
                }
            }
            if(nums[0] >= minStep) return true;
            else return false;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.