Trains the technique from
LeetCode 4002Count Valid SequencesThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A strip has n slots in a row, and k markers are to be placed in them, at most one per slot.
No two markers may sit in neighbouring slots.
Return how many arrangements there are, modulo 10^9 + 7.
Example 1
Three markers over nine slots with none touching leaves seven slots to choose three positions from, which is 35 arrangements.
Example 2
The only arrangement puts markers in the first and last slots.
Example 3
Two markers cannot both fit in two neighbouring slots, so there is no arrangement.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def count_valid_sequences(n: int, k: int) -> int:public int countValidSequences(int n, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.