O(n) Greedy Java solution using a while loop


  • 0
    E
    public boolean canJump(int[] nums) {
            int index = 0;
            int jumpsLeft = nums[0];
            
            while(index < nums.length - 1 && jumpsLeft > 0) {
                jumpsLeft = Math.max(--jumpsLeft, nums[++index]);
            }
            
            return index == nums.length - 1;
        }
    

Log in to reply
 

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