Loading...
You are given a list of distinct integers blocked and three integers forward, backward, and target. You start at position 0 on a number line and want to land on position target.
Each move is one jump of one of two kinds:
p to position p + forward.p to position p - backward.Every jump must follow these rules:
blocked.You may jump past target and come back to it later.
Return the minimum number of jumps needed to land on target. If there's no solution, return -1.
Input: blocked = [14,4,18,1,15], forward = 3, backward = 15, target = 9
Output: 3
Explanation: Three forward jumps go 0 -> 3 -> 6 -> 9, and none of 3, 6, or 9 is blocked. Fewer jumps can't work. One jump reaches only 3. Two jumps reach only 6, because a backward jump from 3 would land on -12.
Input: blocked = [8,3,16,6,12,20], forward = 15, backward = 13, target = 11
Output: -1
Explanation: After i forward jumps and j backward jumps, the position is 15 * i - 13 * j. It equals 11 only when j > i (the smallest case is i = 12, j = 13). Every backward jump must come right after a forward jump, so a valid sequence never has more backward jumps than forward jumps. So 11 can't be reached.
Input: blocked = [1,6,2,14,5,17,4], forward = 16, backward = 9, target = 7
Output: 2
Explanation: A forward jump goes past the target to 16, then a backward jump lands on 16 - 9 = 7. One jump can't work, since the only position one jump reaches is 16.
Input: blocked = [1,6], forward = 3, backward = 2, target = 4
Output: -1
Explanation: The first jump lands on 3. From 3, a forward jump would land on 6 and a backward jump would land on 1. Both are blocked, so no jump can leave 3.
blocked.length ≤1000forward, backward, blocked[i] ≤2000target ≤2000blocked are distinct.target does not appear in blocked.