FastPrepMinimum Jumps to Reach Home

Minimum Jumps to Reach Home

GoodScore logoGoodScore● MediumFULLTIMEPHONE SCREEN
Learn

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) → int

Examples

Example 1

forbidden = [14,4,18,1,15]forward = 3backward = 15target = 9return = 3

Case 1 exercises the documented deterministic contract.

Example 2

forbidden = [8,3,16,6,12,20]forward = 15backward = 13target = 11return = -1

Case 2 exercises the documented deterministic contract.

Example 3

forbidden = [1,6,2,14,5,17,4]forward = 16backward = 9target = 7return = 2

Case 3 exercises the documented deterministic contract.

Constraints

  • 0 <= forbidden.length <= 2000.
  • 1 <= forward, backward, target <= 2000.
  • 1 <= forbidden[i] <= 2000, with unique forbidden positions.

More GoodScore problems

See GoodScore hiring insights
public int minimumJumps(int[] forbidden, int forward, int backward, int target) {
    // Write your code here.
}
forbidden[14,4,18,1,15]
forward3
backward15
target9
expected3
Checking account…