Dynamic Programming21 sections · 916 units
Open in Course

Consecutive Ones - The Setup

Working in binary

Convert nn to binary. Now each "digit" is 00 or 11. At each position, you choose 00 or 11 (subject to tight bound). The constraint: no two consecutive 11s. You need to track the previous bit.

If the previous bit was 11, you can't place 11 again. State: (pos,tight,prevBit)(pos, tight, prevBit). The prevBitprevBit tells you whether placing 11 at this position would create 1111. Understanding the problem structure is half the battle.