5 lines Java Solution with O(1) space O(n) time


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

    }


  • 0
    G

    The last line can be changed to "return false;" as "front >= nums.length-1" case is always captured inside for loop.


  • 0
    F
        int cur = 0, farest = 0;
        while (cur <= farest && farest < (nums.length - 1)) {
            farest = Math.max(farest, nums[cur] + cur);
            cur++;
        }
        return farest >= (nums.length - 1);

Log in to reply
 

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