Skip to content
CodeShare
All problems

Jump Game

Can you hop from the first index to the last?

MediumArraysGreedy

You start at the first index of the array nums. Each element is your maximum jump length from that position.

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

Examples

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 lands on index 3, whose jump length is 0.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5