Java O(n) Solution with explanations.

  • 0

    The description of the question tells us: If one index cannot be reached, all the latter indexes cannot be reached. Since of that, what should be done is very simple, track the max index you can reach.

    public boolean canJump(int[] nums) {
        if (nums == null || nums.length == 0) return false;
        int maxReach = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if ( i > maxReach ) return false;
            maxReach = Math.max(maxReach, i + nums[i]);
        return true;

Log in to reply

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