My concise greedy strategy


  • 0
    J

    Assume that we can move step = nums[0] steps at the beginning, then move to num[1] and step--, always take the max of num[1] and step as remaining steps, and repeat until arriving at the last element. If step == 0 during the iteration, it means we could neither move forward nor reach the end, so return false.

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

Log in to reply
 

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