iq.lab
Python starts when a code cell comes near or you run one
mediumGreedy choice target 25 min

Jump game

You stand on position 0 of a list nums of non-negative integers. Each number is the longest jump you may make from its position: from position i you may move forward to any position from i + 1 up to i + nums[i]. A 0 means you cannot move on from there.

Return True if some sequence of jumps reaches the last position, and False otherwise. You choose each jump's length, so a big number never forces you past the end.

Example 1
Inputnums = [2, 3, 0, 1, 4]OutputTrue

Jump 1 place to position 1, then 3 places to position 4, the last one.

Example 2
Inputnums = [1, 2, 0, 0, 3]OutputFalse

Positions 0 and 1 reach at most position 3. Positions 2 and 3 hold 0, so position 4 is out of reach.

Example 3
Inputnums = [0]OutputTrue

You already stand on the last position.

Constraints
  • 1 ≤ len(nums) ≤ 105

  • 0 ≤ nums[i] ≤ 105

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.