Ctrl K

Fewest Jumps to the End

Medium
You have a row of cells, and cell i holds lengths[i], the longest forward jump allowed from it. From cell i you may land on any cell from i + 1 to i + lengths[i] as long as you stay inside the row. You start on cell 0. Find the smallest number of jumps needed to land on the last cell. The last cell is guaranteed to be reachable.
Example 1
lengths=[3, 1, 1, 4, 0, 2, 1] → 2

Jump from cell 0 to cell 3, then straight to the last cell, 6. Two jumps.

Example 2
lengths=[1, 1, 1, 1] → 3

Every jump moves exactly one cell, three in total.

Example 3
lengths=[5] → 0

There is only one cell, so no jumps are needed.

Constraints
  • 1 ≤ len(lengths) ≤ 10⁵
  • 0 ≤ lengths[i] ≤ 10⁵
  • The last cell can be reached from cell 0
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsReach the Last Cell MediumAs Many Trades as You Like MediumLongest Seesaw Subsequence MediumFewest Meetings to Cancel MediumLongest Chain of Segments Medium
loading Python…
Input
lengths=[3, 1, 1, 4, 0, 2, 1]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 39 tests on the server, hidden ones included.