#45

Jump Game II

Medium BFS by ranges

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Same jumps as Jump Game, and the end is always reachable. Return the minimum number of jumps to reach the last index.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the fewest jumps to the end.

Why this works: think in levels: all indexes reachable in j jumps form a range; the next range ends at the furthest point reachable from it.

Example: [2, 3, 1, 1, 4]

jump 1 covers indexes 1..2from there furthest 4 → jump 2 → done

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Looping up to the last index inclusive and counting an extra jump when you're already there.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Greedy
🎬 Episodes 69–73: video coming soon