Dynamic Programming21 sections · 916 units
Open in Course

Lessons from Bitmask DP

summary

Four patterns you've seen:

1.1. State compression: When tracking "which elements used," represent it as a bitmask. The state space becomes O(2n)O(2^n) subsets.

2.2. Transitions via bit operations: Set bits, clear bits, check bits. These run in O(1)O(1) and keep your code clean.

3.3. Assignment problems: When matching nn items to nn slots in order, dp[mask]dp[\text{mask}] alone suffices. Process items one by one.

4.4. SOS DP: When you need sums over all subsets (or supersets), don't iterate submasks naively. Use the O(n2n)O(n \cdot 2^n) technique. The constraint n20n \leq 20 is your signal. Anything larger and 2n2^n won't fit in time or memory.