Jump Game
MediumGreedy
You are given an integer array nums. You start at index 0, and nums[i] is the maximum number of positions you can jump forward from index i (you may jump any distance from 1 up to nums[i]; a value of 0 means you cannot move from there).
Return True if you can reach the last index, otherwise False.
Example 1
- Input:
- nums = [1, 3, 0, 0, 2, 0]
- Output:
- True
- Explanation
- Jump 1 to index 1, then jump 3 to index 4, skipping both zeros. From index 4 a jump of 1 reaches the last index, 5.
Example 2
- Input:
- nums = [2, 1, 0, 3]
- Output:
- False
- Explanation
- From index 0 you can reach index 1 or 2, and index 1 also only reaches index 2. Index 2 holds
0, so you are stuck before the3at index 3.
Example 3
- Input:
- nums = [0]
- Output:
- True
- Explanation
- There is only one index, so you already stand on the last one.
Constraints
- 1 ≤
nums.length≤ 104 - 0 ≤
nums[i]≤ 105