Dynamic programming · Easy
A board-game simulator has positions numbered from zero to n. A player jumps forward by one or two positions and cannot land on blocked positions. The task counts distinct ordered jump sequences, not different sets of visited positions.
Return the exact number of valid one-or-two-step jump sequences from position 0 to n.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "n": number, "blocked": number[] }.
Fields
- n is the destination index. blocked lists forbidden landing positions in 0..n; repeated positions are harmless.
Output
Return value
- Return an integer count. If 0 or n is blocked return 0.
- At n=0 with an unblocked start return 1 for the empty sequence.
- Jumping over a blocked position is permitted.
Constraints
- 0 <= n <= 75 and blocked.length <= 76.
- This replaces the unsound previous bound n=100000: with no blockers the exact answer would exceed the JSON safe-integer range. At n=75 the answer is 3416454622906707.
Examples
Example 1
{
"blocked": [
2
],
"n": 4
}Returns
1The sole route is 0→1→3→4.
Example 2
{
"blocked": [],
"n": 0
}Returns
1One empty sequence.
Required complexity
O(n + b) time and O(b) extra space for blocked lookup