Skip to content

55. Jump Game

Description

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.

Return true if you can reach the last index, or false otherwise.

 

Example 1:

Input: nums = [2,3,1,1,4]
Output: true
Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2:

Input: nums = [3,2,1,0,4]
Output: false
Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.

 

Constraints:

  • 1 <= nums.length <= 104
  • 0 <= nums[i] <= 105

Solutions

Solution 1: Greedy

Thinking

The first idea is DFS/BFS over reachable indices, or DP for whether each index is reachable. Correct, but worst-case \(O(n^2)\). \(n \le 10^4\) is tight, and we only care about the last index, not the path.

The waste is expanding every jump. Reachable indices form a prefix: maintain the farthest reachable \(mx\); if some \(i > mx\), we are cut off.

So we scan left to right, update \(mx\) with \(i + \textit{nums}[i]\), and finishing the scan means the end is reachable.

We use a variable \(mx\) to maintain the farthest index that can currently be reached, initially \(mx = 0\).

We traverse the array from left to right. For each position \(i\) we traverse, if \(mx < i\), it means that the current position cannot be reached, so we directly return false. Otherwise, the farthest position that we can reach by jumping from position \(i\) is \(i+nums[i]\), we use \(i+nums[i]\) to update the value of \(mx\), that is, \(mx = \max(mx, i + nums[i])\).

At the end of the traversal, we directly return true.

The time complexity is \(O(n)\), where \(n\) is the length of the array. The space complexity is \(O(1)\).

Similar problems:

1
2
3
4
5
6
7
8
class Solution:
    def canJump(self, nums: List[int]) -> bool:
        mx = 0
        for i, x in enumerate(nums):
            if mx < i:
                return False
            mx = max(mx, i + x)
        return True
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
    public boolean canJump(int[] nums) {
        int mx = 0;
        for (int i = 0; i < nums.length; ++i) {
            if (mx < i) {
                return false;
            }
            mx = Math.max(mx, i + nums[i]);
        }
        return true;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int mx = 0;
        for (int i = 0; i < nums.size(); ++i) {
            if (mx < i) {
                return false;
            }
            mx = max(mx, i + nums[i]);
        }
        return true;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func canJump(nums []int) bool {
    mx := 0
    for i, x := range nums {
        if mx < i {
            return false
        }
        mx = max(mx, i+x)
    }
    return true
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function canJump(nums: number[]): boolean {
    let mx: number = 0;
    for (let i = 0; i < nums.length; ++i) {
        if (mx < i) {
            return false;
        }
        mx = Math.max(mx, i + nums[i]);
    }
    return true;
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
impl Solution {
    #[allow(dead_code)]
    pub fn can_jump(nums: Vec<i32>) -> bool {
        let n = nums.len();
        let mut mx = 0;

        for i in 0..n {
            if mx < i {
                return false;
            }
            mx = std::cmp::max(mx, i + (nums[i] as usize));
        }

        true
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
/**
 * @param {number[]} nums
 * @return {boolean}
 */
var canJump = function (nums) {
    let mx = 0;
    for (let i = 0; i < nums.length; ++i) {
        if (mx < i) {
            return false;
        }
        mx = Math.max(mx, i + nums[i]);
    }
    return true;
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
public class Solution {
    public bool CanJump(int[] nums) {
        int mx = 0;
        for (int i = 0; i < nums.Length; ++i) {
            if (mx < i) {
                return false;
            }
            mx = Math.Max(mx, i + nums[i]);
        }
        return true;
    }
}

Comments