Dynamic Programming21 sections · 916 units
Open in Course

Special Integers - State Definition

Adding the mask

Define dp(pos,tight,started,mask)dp(pos, tight, started, mask) = count of valid special integers from position pospos onward. maskmask: bitmask of used digits. Bit dd is 11 if digit dd has been placed.

Why "started"? Leading zeros don't count as using digit 00. The number 042042 is 4242, and 00 isn't "used." You only start tracking the mask once you place a real digit. Choosing what dp tracks is often the hardest part. Each dp value must have enough information to count valid numbers from that position onward.