Watch 6 video solutions for Minimum Jumps to Reach Home, a medium level problem involving Array, Dynamic Programming, Breadth-First Search. This walkthrough by Happy Coding has 10,203 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
A certain bug's home is on the x-axis at position x. Help them get there from position 0.
The bug jumps according to the following rules:
a positions forward (to the right).b positions backward (to the left).forbidden positions.The bug may jump forward beyond its home, but it cannot jump to positions numbered with negative integers.
Given an array of integers forbidden, where forbidden[i] means that the bug cannot jump to the position forbidden[i], and integers a, b, and x, return the minimum number of jumps needed for the bug to reach its home. If there is no possible sequence of jumps that lands the bug on position x, return -1.
Example 1:
Input: forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9 Output: 3 Explanation: 3 jumps forward (0 -> 3 -> 6 -> 9) will get the bug home.
Example 2:
Input: forbidden = [8,3,16,6,12,20], a = 15, b = 13, x = 11 Output: -1
Example 3:
Input: forbidden = [1,6,2,14,5,17,4], a = 16, b = 9, x = 7 Output: 2 Explanation: One jump forward (0 -> 16) then one jump backward (16 -> 7) will get the bug home.
Constraints:
1 <= forbidden.length <= 10001 <= a, b, forbidden[i] <= 20000 <= x <= 2000forbidden are distinct.x is not forbidden.Problem Overview: You start at position 0 on a number line and want to reach position x. You can jump forward by a or backward by b, but some positions are forbidden and you cannot make two backward jumps in a row. The task is to compute the minimum number of jumps required to reach x.
Approach 1: Brute Force DFS / Backtracking (Exponential Time)
The most direct idea is to recursively explore every possible sequence of forward and backward jumps. From each position, try moving forward by a and backward by b while skipping forbidden positions and preventing consecutive backward moves. This search forms a tree where nodes represent positions. Without strict bounds and memoization, the exploration quickly revisits the same states and grows exponentially.
Even with a visited set, DFS is inefficient for shortest-path problems because it explores deep paths before discovering shorter ones. The time complexity can grow to O(2^n) in the worst case with space complexity O(n) for recursion and state tracking. This approach mainly helps understand the state constraints but is not practical for large inputs.
Approach 2: Breadth-First Search with State Tracking (O(n))
The problem naturally maps to a shortest-path search on a graph. Each position is a node, and edges represent valid jumps. Breadth-First Search works well because it explores positions level by level, guaranteeing the first time you reach x is with the minimum number of jumps.
The tricky part is the rule that disallows two consecutive backward jumps. Because of that, the state must include both the current position and whether the previous jump was backward. A visited structure like visited[position][backUsed] prevents revisiting identical states. Forward jumps are always allowed if the position is valid. Backward jumps are only allowed if the previous move was not backward.
To avoid infinite exploration to the right side of the number line, apply a practical boundary such as max(forbidden) + a + b + x. This keeps the search space finite. BFS processes each state at most once, giving O(n) time complexity with O(n) space for the queue and visited set. The implementation uses arrays and queues, which aligns with common patterns in array traversal and state exploration.
Recommended for interviews: Interviewers expect the BFS state-tracking approach. Recognizing the problem as a shortest-path search with extra state (previous move direction) demonstrates strong understanding of BFS and constrained graph traversal. Mentioning the brute force DFS approach first shows you understand the search space, but implementing BFS with visited states is the practical and optimal solution.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS / Backtracking | O(2^n) | O(n) | Conceptual exploration of the state space; not suitable for large constraints |
| Breadth-First Search with State Tracking | O(n) | O(n) | Best approach for shortest jumps with movement constraints and forbidden positions |