↓Skip to main content
  1. LeetCode/

LeetCode 45: Jump Game II

·3 mins· ·
LeetCode Medium Greedy Bfs
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-05-01 Source: Day 16 learning note

Study Context
#

This article is rebuilt from the exact LeetCode section in the learning note. I kept the note’s repair points, comparison points, and common mistakes, while removing unrelated non-LeetCode material from the same day.

Related Reminders From The Note#

    1. Explain current_end vs farthest in LC 45.
    1. Re-explain why LC 45 is greedy instead of defaulting to DP language.

Learning Note Extract
#

Problem 2 - LC 45 Jump Game II
#

  • Status: Good enough after BFS-layer greedy correction.
  • Pattern: Greedy / BFS-layer frontier expansion.

Why Greedy Fits
#

This problem asks for:

minimum number of jumps

The clean way to think about it is BFS by layers:

  • all indices up to current_end are reachable with the current number of jumps
  • while scanning that layer, compute the farthest index reachable with one more jump
  • when the layer ends, commit one jump

Core Invariants
#

current_end = farthest index reachable with current jump count
farthest = farthest index reachable while scanning the current layer
jumps = number of committed layers / jumps

Layer Transition
#

for i in range(len(nums) - 1):
    farthest = max(farthest, i + nums[i])
    if i == current_end:
        jumps += 1
        current_end = farthest

Complexity
#

Time: O(n)
Space: O(1)

Common Mistakes
#

  • defaulting to O(n^2) DP even though a linear greedy solution exists
  • incrementing jumps when farthest changes instead of when the current layer ends
  • iterating through the last index and adding one unnecessary jump

Interview-Ready Explanation
#

I treat the array like BFS layers. current_end is the farthest index reachable with the current number of jumps, and farthest is the farthest position I can reach while scanning that layer. When I finish the layer, I increment jumps and move current_end to farthest.

Clean Solution
#

The note above captures the reasoning and the mistakes to avoid. The implementation below is the version I would submit.

from typing import List

class Solution:
    def jump(self, nums: List[int]) -> int:
        jumps = 0
        current_end = 0
        farthest = 0

        for i in range(len(nums) - 1):
            farthest = max(farthest, i + nums[i])
            if i == current_end:
                jumps += 1
                current_end = farthest

        return jumps

Complexity
#

Time O(n), Space O(1).

Mistakes To Watch
#

  • Doing O(n^2) DP unnecessarily.
  • Incrementing jumps after reaching the last index.

Final Interview Explanation
#

Start from the state definition, then explain why the transition preserves that state. If there is a loop direction, state compression, or a similar-looking problem with a different answer shape, call that out explicitly because that is where this problem family usually breaks down.

Related

LeetCode 55: Jump Game
·2 mins
LeetCode Medium Greedy
LeetCode note for Jump Game, rebuilt from the original learning note
LeetCode 3914: Minimum Operations to Make Array Non Decreasing
·2 mins
LeetCode Medium Greedy Array
LeetCode 3914 note: adding x to a range raises only the difference at its left edge by x, so the answer is the sum of all drops
LeetCode 3905: Multi Source Flood Fill
·2 mins
LeetCode Medium Bfs Matrix
LeetCode 3905 note: multi-source BFS one layer at a time, collecting the maximum color per cell before writing the layer back
LeetCode 3555: Smallest Subarray to Sort in Every Sliding Window
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 3555 note: every length-k window is one instance of LeetCode 581; with n only 1000, two O(k) scans per window are enough
LeetCode 581: Shortest Unsorted Continuous Subarray
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array Monotonic-Stack
LeetCode 581 note: scanning left to right with a running max, the last index below it is the right boundary; scanning right to left with a running min, the last index above it is the left boundary
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·3 mins
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 note: process the “it” players left to right, each catching the leftmost uncaught person in range, with a right-only pointer to the next candidate