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

LeetCode 1306: Jump Game III

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

基本資料
#

難易度: medium 第一次嘗試:2026-01-05

  • 提交紀錄:Accepted(一次通過)

解題思路
#

在 index i 可以跳到 i + arr[i] 或 i - arr[i]。問從 start 出發,能不能跳到任何一個值為 0 的位置。

把每個 index 當成圖的節點、兩種跳法當成邊,就是可達性問題:BFS 或 DFS 都可以,記得標記走過的位置,避免來回跳。

我的解法
#

原本是遞迴 DFS:

def dfs(idx):
    if arr[idx] == 0:
        return True
    reach[idx] = True
    res = False
    forward = idx + arr[idx]
    if forward < n and not reach[forward]:
        res |= dfs(forward)
    backward = idx - arr[idx]
    if backward >= 0 and not reach[backward]:
        res |= dfs(backward)
    return res

可以 AC,但 n 到 5 * 10^4,遞迴深度可能超過 Python 的上限。另外 res |= ... 在找到之後還會繼續探索另一邊,可以直接回傳。改成 BFS:

from collections import deque

class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:
        n = len(arr)
        seen = [False] * n
        seen[start] = True
        q = deque([start])
        while q:
            i = q.popleft()
            if arr[i] == 0:
                return True
            for j in (i + arr[i], i - arr[i]):
                if 0 <= j < n and not seen[j]:
                    seen[j] = True
                    q.append(j)
        return False

複雜度
#

時間 O(n),空間 O(n)。

要特別避免的錯誤
#

  • 沒有標記走過的位置:會在兩個 index 之間來回跳,無限迴圈。
  • 遞迴 DFS 在 n 很大時超過遞迴上限。

面試口說整理
#

我把每個 index 當成節點,往左右跳是兩條邊,問題就變成從 start 能不能走到值為 0 的節點。用 BFS 加上 seen 陣列,每個 index 最多進出 queue 一次,遇到 0 就回傳 True,時間 O(n)。

相關文章

LeetCode 3619: Count Islands With Total Value Divisible by K
·2 分鐘
LeetCode Medium Dfs Bfs Matrix Graph
LeetCode 3619 解題筆記:網格 flood fill 加總每座島的值;格子到 10^5,用迭代避免遞迴太深
LeetCode 3493: Properties Graph
·1 分鐘
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 解題筆記:先把每列轉成 set,兩兩比較交集大小建邊,再數連通塊
LeetCode 2653: Sliding Subarray Beauty
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 解題筆記:值域只有 -50 到 50,固定視窗用計數陣列維護,每個視窗從 -50 往上累加找第 x 小的負數
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·1 分鐘
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 解題筆記:求最短的可變視窗;同一個值只算一次,記錄每個值最後出現的位置,左邊界移出的是最後一次出現才扣掉
LeetCode 3796: Find Maximum Value in a Constrained Sequence
·1 分鐘
LeetCode Medium Greedy Array
LeetCode 3796 解題筆記:每個位置的上界是自己的限制、左邊上界 + 差值、右邊上界 + 差值三者取最小;左右各掃一次
LeetCode 3788: Maximum Score of a Split
·1 分鐘
LeetCode Medium Prefix-Sum Array
LeetCode 3788 解題筆記:先建後綴最小值,再從左邊累加前綴和,枚舉每個分割點