Dynamic Programming21 sections · 916 units
Open in Course

Consecutive Ones - State Definition

Tracking previous bit

Define dp(pos,tight,prev)dp(pos, tight, prev) = count of valid binary numbers from position pospos onward, given the previous bit was prevprev. prev=1prev = 1: the last placed bit was 11.

You can only place 00 now. prev=0prev = 0: the last placed bit was 00 (or no bit yet). You can place 00 or 11. Base case: when pospos reaches the end, return 11. You've built a valid number. This state captures all the information needed to count valid binary numbers from position pos onward.