#45
Medium
Greedy

Jump Game II

Find the minimum number of jumps to reach the last index.

ArrayGreedy

Pattern fit

The greedy idea is to treat one jump as one BFS layer and keep the farthest boundary reachable inside the current layer.

Key observation

You only increment the jump count when the current layer ends, not every time you see a farther reach.

Target complexity

O(n) / O(1)

How to break down the solution cleanly

1

The greedy idea is to treat one jump as one BFS layer and keep the farthest boundary reachable inside the current layer.

2

You only increment the jump count when the current layer ends, not every time you see a farther reach.

3

Identify the local choice that seems to preserve the most future flexibility.

4

Sort or preprocess if that makes the choice testable.

Reference implementation

Python
# Generic pattern template
items.sort(key=priority)
answer = 0
for item in items:
    if can_take(item):
        take(item)
        answer = update(answer)

Common pitfalls

warning

Incrementing jumps too early inside the same layer.

warning

Not resetting the next-layer farthest boundary correctly.

Common follow-ups

Why is this greedy equivalent to BFS layering?

How would you detect an unreachable end position?

Continue with related problems

Build repeatable depth inside the Greedy cluster before moving on.

view_weekBack to the pattern page
LeetCode 45. Jump Game II Guide | Interview AiBox