Dynamic Programming21 sections · 916 units
Open in Course

Consecutive Ones - Transition

Avoiding 11

At position pospos:

1.1. You can always place 00. Recurse with prev = 00.

2.2. You can place 11 only if prev 1\neq 1 and (not tight or n[pos]=1n[pos] = 1). If prev was 11 and you try to place 11, you'd create 1111. Skip that branch. The tight logic works the same as base 1010: if tight and n[pos]=0n[pos] = 0, you can't place 11. This formula tells you how to compute dp(pos, tight, prev) from dp values at later positions. Getting this right is the core of DP.