Minimum Jumps to Reach Home
Problem statement
Start at position 0. A forward jump adds forward; a backward jump subtracts backward. Positions cannot become negative, forbidden positions cannot be visited, and two backward jumps cannot occur consecutively.
Return the minimum jumps needed to reach target, or -1 if it is unreachable.
Function
minimumJumps(forbidden: int[], forward: int, backward: int, target: int) → intExamples
Example 1
forbidden = [14,4,18,1,15]forward = 3backward = 15target = 9return = 3Case 1 exercises the documented deterministic contract.
Example 2
forbidden = [8,3,16,6,12,20]forward = 15backward = 13target = 11return = -1Case 2 exercises the documented deterministic contract.
Example 3
forbidden = [1,6,2,14,5,17,4]forward = 16backward = 9target = 7return = 2Case 3 exercises the documented deterministic contract.
Constraints
0 <= forbidden.length <= 2000.1 <= forward, backward, target <= 2000.1 <= forbidden[i] <= 2000, with unique forbidden positions.