This might be the simplest interview question ever...


  • 5
    Z
    public boolean canJump(int[] A) {
        int max = 0;
        for (int i=0; i<=max && i<A.length; i++) {
            max = Math.max(A[i] + i, max);
        }
        return max >= A.length - 1;
    }
    

    As straightforward as it is. Just update the current max position it can reach. A easy minor optimization is to early terminate when max is already enough big. But this is already enough.


Log in to reply
 

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