Problem · Dynamic Programming
Constrained Jump Sequences
Learn this problemProblem statement
There are n locations on a straight line, numbered from 1 through n. You begin at start, and a fixed spotlight is at spotlight.
Make exactly jumps consecutive jumps. For every jump from the current location x to the next location y, both rules must hold:
x != y.|x - y| < |x - spotlight|.
Return the number of valid jump sequences modulo 10^9 + 7.
Function
countJumpSequences(n: int, start: int, spotlight: int, jumps: int) → intExamples
Example 1
n = 5start = 2spotlight = 4jumps = 1return = 2From location 2, the current distance to the spotlight is 2. The legal destinations are 1 and 3.
Example 2
n = 5start = 2spotlight = 4jumps = 2return = 2The first jump can reach 1 or 3. Location 3 has no legal next destination, while location 1 can jump to 2 or 3. Therefore there are two sequences.
Example 3
n = 6start = 5spotlight = 3jumps = 2return = 2The only valid two-jump sequences are 5 -> 6 -> 4 and 5 -> 6 -> 5.
Constraints
2 <= n <= 20001 <= start, spotlight <= n1 <= jumps <= 2000