An O(n) Solution in Java


  • 0
    H
    public boolean canJump(int[] nums) {
        if (nums == null || nums.length <= 1) {
            return nums == null || nums.length == 0? false: true;
        }
        int i = nums.length - 2;
        int pre = nums.length;
        int minJump = 1;
        while (i > 0) {
            if (nums[i] >= minJump) {
                minJump = 1;
                pre = i;
            } else {
                minJump = pre - i + 1;
            }
            i--;
        }
        return nums[i] >= minJump;
    }

Log in to reply
 

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