Dynamic Programming21 sections · 916 units
Open in Course

Special Integers - The Setup

Tracking used digits

to make sure all digits are distinct, you must remember which digits you've already placed. A digit can only appear once. How do you track "used digits"? A bitmask.

If bit dd is set, digit dd has been used. Ten digits means a 10-bit mask. When placing digit dd, check if bit dd is already set. If yes, skip. If no, set bit dd and continue. Take time to understand the problem structure. The recursive relationship often becomes clear once you see how smaller instances relate to larger ones.