↓Skip to main content
  1. LeetCode/

LeetCode 1306: Jump Game III

·2 mins· ·
LeetCode Medium Bfs Dfs Graph Array
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-01-05

  • Submissions: Accepted (first try)

Approach
#

From index i you can jump to i + arr[i] or i - arr[i]. Can you reach any index holding 0 from start?

With indices as nodes and the two jumps as edges, this is reachability: BFS or DFS both work, as long as visited indices are marked to avoid jumping back and forth.

My Solution
#

The original was a recursive 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

It passes, but with n up to 5 * 10^4 the recursion can exceed Python’s limit. Also, res |= ... keeps exploring the other side after finding a zero, where it could return immediately. A BFS version:

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

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Not marking visited indices: the search bounces between two indices forever.
  • Recursive DFS exceeding the recursion limit for large n.

Interview Explanation
#

I treat each index as a node with two edges, the left and right jumps, so the question is whether a node with value 0 is reachable from start. BFS with a seen array visits each index at most once and returns True on reaching a zero, in O(n) time.

Related

LeetCode 3619: Count Islands With Total Value Divisible by K
·2 mins
LeetCode Medium Dfs Bfs Matrix Graph
LeetCode 3619 note: flood fill each island and sum its values; with up to 10^5 cells, iterate to avoid deep recursion
LeetCode 3493: Properties Graph
·2 mins
LeetCode Medium Graph Dfs Union-Find Hash-Map
LeetCode 3493 note: convert each row to a set, compare intersection sizes pairwise to add edges, then count components
LeetCode 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
LeetCode 3796: Find Maximum Value in a Constrained Sequence
·2 mins
LeetCode Medium Greedy Array
LeetCode 3796 note: each position’s bound is the min of its own limit, the left bound + difference and the right bound + difference; one pass each way
LeetCode 3788: Maximum Score of a Split
·1 min
LeetCode Medium Prefix-Sum Array
LeetCode 3788 note: build the suffix minimum first, then accumulate the prefix sum from the left and try every split