JAVA Easy Solution with DP


  • 0
    J
    public class Solution {
        public boolean canJump(int[] nums) {
            if (nums == null || nums.length == 0) return true;
            int len = nums.length, j = 0, last = len;
            boolean []dp = new boolean[len + 1];
            dp[len] = true;
            
            for (int i = len - 2; i >= 0; i--) {
                if (dp[i+2] && nums[i] > 0) {
                    dp[i+1] = true;
                    last = i + 1;
                }
                else if (!dp[i+2]) {
                  
                    if (last - (i + 1) <= nums[i]) {
                        dp[i+1] = true;
                        last = i + 1;
                    }
                }
            }
            
            return dp[1];
        }
    }

Log in to reply
 

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