All problems
0340MediumDynamic ProgrammingPrefix Sum

Settled Control Tapes

Tracked in this browser only
Write code

Trains the technique from

LeetCode 3129Find All Possible Stable Binary Arrays I

This 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.

Examples

Example 1

Input
zero = 2, one = 2, limit = 1
Output
2

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

Input
zero = 2, one = 3, limit = 2
Output
7

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

Input
zero = 4, one = 3, limit = 7
Output
35

`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.

Constraints

  • 1 <= zero <= 200
  • 1 <= one <= 200
  • 1 <= limit <= 200

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def number_of_stable_arrays(zero: int, one: int, limit: int) -> int:
Java
public int numberOfStableArrays(int zero, int one, int limit)
September 7
Apply