All problems

Jump Game

Medium · greedy

You are standing on the first square of a row of squares, one per item of nums. The number nums[i] is the longest jump you can make from square i: from there you may move forward any distance from 1 up to nums[i] squares, or not move at all.

Return True if you can reach the last square, and False if you will always get stuck somewhere before it.

Examples

Example 1

Input: nums = [2, 3, 1, 1, 4]

Output: True

Jump 1 square to index 1, then 3 squares to the last index.

Example 2

Input: nums = [3, 2, 1, 0, 4]

Output: False

Every path lands on index 3, where the jump length is 0, so index 4 can never be reached.

Example 3

Input: nums = [0]

Output: True

You are already on the last square.

Constraints