Practice Minimum Jumps to Reach Home
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
A bug starts at position 0 and wants to reach x. It may jump exactly a positions forward or b positions backward, but may not jump backward twice consecutively. It may not land on any forbidden position or any negative position; jumping past x is allowed. Return the fewest jumps to reach x exactly, or -1 if impossible.
Source: Minimum Jumps to Reach Home on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
forbidden = [14,4,18,1,15], a = 3, b = 15, x = 9
Output
3
# 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
Constraints
1 <= forbidden.length <= 1000
1 <= a, b, forbidden[i] <= 2000
0 <= x <= 2000
All the elements in forbidden are distinct.
Position x is not forbidden.
Related LeetCode Problems
Practice more problems similar to Minimum Jumps to Reach Home.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.