Trains the technique from
LeetCode 3129Find All Possible Stable Binary Arrays IThis 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 punch machine writes a control tape one cell at a time. Each cell ends up holding either the value 0 or the value 1, and the finished tape must hold exactly zero cells of value 0 and exactly one cells of value 1, so its length is always zero + one.
The reader that consumes the tape loses synchronisation if it ever passes a long stretch of identical cells. A tape is therefore called settled when every maximal block of equal neighbouring cells holds at most limit cells; equivalently, nowhere on the tape do limit + 1 consecutive cells carry the same value.
Two tapes are different when they differ in at least one position. Count the settled tapes and report that count as a remainder after division by 1000000007.
Example 1
Writing a tape as its run of cell values, the settled tapes are `0101` and `1010`. Every block in each one holds a single cell, which is within `limit = 1`.
Example 2
The seven settled tapes are `01011`, `01101`, `10011`, `10101`, `10110`, `11001` and `11010`. Each holds two `0` cells and three `1` cells, and no block in any of them is longer than two.
Example 3
`limit = 7` is at least the tape length, so no arrangement can violate the block rule and every ordering of four `0` cells and three `1` cells counts.
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 number_of_stable_arrays(zero: int, one: int, limit: int) -> int:public int numberOfStableArrays(int zero, int one, int limit)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.