1306. Jump Game III

  • Array
  • BFS
  • DFS
TIME
O(n)
SPACE
O(n)
problem on leetcode ↗source on github ↗

Intuition

Treat each index as a node in a graph. From index i, there are at most two outgoing edges:

  • i + arr[i]
  • i - arr[i]

The question is whether any reachable node has value 0.

Because cycles are possible, we need a visited array.

Approach

  1. Start BFS from start.
  2. Mark start as visited.
  3. While the queue is not empty:
    • pop an index
    • if arr[index] == 0, return true
    • try both next indices
    • enqueue valid, unvisited indices
  4. If BFS finishes without finding 0, return false.

Complexity

  • Time: O(n), each index is visited at most once
  • Space: O(n), for the queue and visited array

Edge Cases

  • arr[start] == 0 -> return true
  • Jump value is 0 at a non-start index
  • Cycles between indices -> handled by visited
  • Jumps outside bounds -> ignored

Code

from collections import deque
from typing import List


class Solution:
    def canReach(self, arr: List[int], start: int) -> bool:
        """
        1306. Jump Game III
        Time: O(n)
        Space: O(n)
        """
        n = len(arr)
        visited = [False] * n
        queue = deque([start])
        visited[start] = True

        while queue:
            index = queue.popleft()

            if arr[index] == 0:
                return True

            for next_index in (index + arr[index], index - arr[index]):
                if 0 <= next_index < n and not visited[next_index]:
                    visited[next_index] = True
                    queue.append(next_index)

        return False