Jump Game
MediumP1Coding
You are given an array nums where nums[i] is the maximum jump length from index i. You start at index 0. Return true if you can reach the last index, otherwise false.
From index i you may jump any distance from 0 up to nums[i]. A one-element array is already at the last index.
Example 1
- Input
- nums = [2,3,1,1,4]
- Output
- true
- Why
- Jump 1 step to index 1, then 3 steps to the last index.
Example 2
- Input
- nums = [3,2,1,0,4]
- Output
- false
- Why
- Every path reaches index 3, where the jump length is 0.
Example 3
- Input
- nums = [0]
- Output
- true
Constraints
- 1 <= nums.length <= 10^4
- 0 <= nums[i] <= 10^5
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.