Practice · Greedy · 3 of 4

Reach the Last Index

You start at index 0 of a list of non-negative integers nums. From index i you may jump forward by any number of steps from 1 up to nums[i]; from an index holding 0 you can't move.

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

Example 1

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

Example 2

Input
nums = [3, 1, 1, 0, 5]
Output
false
No jump gets past index 3, which holds 0.

Constraints

  • nums holds 1 to 10⁴ numbers.
  • 0 ≤ nums[i] ≤ 10⁵