↓快轉到主要內容
  1. LeetCode/

LeetCode 45: Jump Game II

·2 分鐘· ·
LeetCode Medium Greedy Bfs
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2026-05-01 來源:Day 16 learning note

學習脈絡
#

這篇是從 learning note 裡該 LeetCode 題目的段落重新整理出來的版本。我保留當天筆記中的修正點、比較點、容易犯錯的地方,並移除同一天其他非 LeetCode 主題,避免文章內容混題。

筆記中提到的相關提醒
#

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

當天筆記摘錄
#

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.

正確解法
#

上面的筆記保留了推理脈絡和當天需要修正的點。下面是我會提交的版本。

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

複雜度
#

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

要特別避免的錯誤
#

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

面試口說整理
#

先講清楚 state definition,再說 transition 為什麼維持這個 state。只要這題有 loop direction、狀態壓縮、或題型相似但 answer shape 不同的地方,就要主動講出來,因為那通常就是這類題最容易出錯的點。

相關文章

LeetCode 55: Jump Game
·2 分鐘
LeetCode Medium Greedy
LeetCode 55 解題筆記,依照原始 learning note 重新整理
LeetCode 3905: Multi Source Flood Fill
·1 分鐘
LeetCode Medium Bfs Matrix
LeetCode 3905 解題筆記:多源 BFS 一次處理一層,先收集這一層每格的最大顏色,再一起寫回網格
LeetCode 1989: Maximum Number of People That Can Be Caught in Tag
·1 分鐘
LeetCode Medium Two-Pointers Greedy Array
LeetCode 1989 解題筆記:鬼從左到右,每個鬼抓範圍內最左邊、還沒被抓的人;用一個只往右走的指針指向下一個候選人
LeetCode 1616: Split Two Strings to Make Palindrome
·2 分鐘
LeetCode Medium Two-Pointers String Greedy
LeetCode 1616 解題筆記:外側用 a 的前綴配 b 的後綴貪心比對,配不下去時,剩下的中間段必須整段在 a 或 b 裡是回文;兩個方向都要檢查
LeetCode 948: Bag of Tokens
·1 分鐘
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 解題筆記:排序後,用最小的 token 換分數、用最大的 token 換能量;只有分數 > 0 才能換能量,換不了就停
LeetCode 3413: Maximum Coins From K Consecutive Bags
·2 分鐘
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 解題筆記:最佳的 k 個袋子,一定有一端對齊某段的端點;分別算「左端對齊段的起點」和「右端對齊段的終點」(鏡像後重用同一個函式)