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.
This approach involves treating each position as a node and using BFS to explore all reachable positions. We track the current position and a boolean indicating whether the last move was backward. This helps in avoiding consecutive backward moves and revisiting positions.
We utilize a queue to explore jumps, marking visited positions to avoid infinite loops. Success is found the first time we reach the home position x.
The solution uses a BFS approach starting from position 0. Each node in the queue keeps track of the current position and if the last move was a backward jump. We explore forward jumps unrestrictedly and backward jumps carefully, considering the constraints.
Time Complexity: O(m + n), Space Complexity: O(m + n)
where m is the maximum allowed position and n is the number of forbidden positions.
We can use the position and jumping direction of the flea as the state, and use BFS to search for the shortest path. The key point of this problem is to determine the right boundary, that is, how far the flea can jump.
If a geq b, that is, the distance to jump forward is greater than the distance to jump backward, then if the flea is in a position greater than x+b, it can no longer jump forward, because the flea cannot jump backward consecutively. If it continues to jump forward, it will never be able to jump to the position x. Therefore, if a geq b, the right boundary can be x+b.
If a < b, that is, the distance to jump forward is less than the distance to jump backward, then if the position of the flea minus b exceeds 2000, choose to jump backward, otherwise jump forward. Therefore, if a < b, the right boundary does not exceed 6000.
In summary, we can set the right boundary to 6000.
Next, we use BFS to search for the shortest path. We use a queue, initially adding the position and jumping direction of the flea as the state to the queue. Each time we take a state from the queue, if the position of this state is equal to x, then we have found a path from the initial state to the target state, and we can return the current number of steps. Otherwise, we add the next state of the current state to the queue. There are two cases for the next state:
1;1, jump backward, the jumping direction is 0.Note that we need to judge whether the next state is legal, that is, the position of the next state does not exceed the right boundary, is not in the forbidden position, and has not been visited.
If the queue is empty, it means that the target position cannot be reached, return -1.
The time complexity is O(M), and the space complexity is O(M). Here, M is the right boundary, and in this problem, M leq 6000.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Breadth-First Search (BFS) with State Tracking | Time Complexity: O(m + n), Space Complexity: O(m + n) |
| BFS | — |
| 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 |
LeetCode 1654. Minimum Jumps to Reach Home • Happy Coding • 10,203 views views
Watch 5 more video solutions →Practice Minimum Jumps to Reach Home with our built-in code editor and test cases.
Practice on FleetCode