Opening the reading…
Opening the reading…
DYNAMIC PROGRAMMING LEVEL 2 › DP WITH BITMASK
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →The assignment must satisfy two conditions at once: every person receives one acceptable T-Shirt, and each T-Shirt is used at most once. The second condition is the difficult one if you assign shirts person by person, because you would need to remember which of up to 100 IDs have already been taken. That history is too large for a direct bitmask.
Reverse the viewpoint. Process T-Shirt IDs from 1 through 100, and decide whether the current shirt is unused or goes to one eligible person. At any point, the only information needed about earlier shirts is which people have already received one. With at most 10 people, that set fits in a 10-bit mask.
After all 100 IDs have been processed, the mask with every bit set represents assignments in which every person received exactly one shirt. Processing each ID once also guarantees that no assignment can use the same shirt twice: the shirt is either skipped or assigned to one person during its own step.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(100 x n x 2^n) | There are 100 shirt steps, 2^n masks, and at most n eligible people to inspect for one shirt and mask. A transition adds one current shirt to one person, so no shirt is processed more than once; when every person accepts every shirt, this reaches the stated worst-case bound. |
| Space | O(2^n) extra | The output count is a single integer and is not working memory. The two mask arrays each hold 2^n values, while the reverse preference lists hold at most 100 x n entries; since n is at most 10 and 100 is fixed, the extra space is O(2^n) for the DP. |
#include <vector>
using namespace std;
class Solution {
public:
int countWays(vector<vector<int>> preferences) {
const int MOD = 1000000007;
int n = static_cast<int>(preferences.size());
int states = 1 << n;
vector<int> eligible[101];
for (int person = 0; person < n; ++person) {
for (int shirt : preferences[person]) {
eligible[shirt].push_back(person);
}
}
vector<int> dp(states, 0);
dp[0] = 1;
for (int shirt = 1; shirt <= 100; ++shirt) {
vector<int> next = dp;
for (int mask = 0; mask < states; ++mask) {
if (dp[mask] == 0) continue;
for (int person : eligible[shirt]) {
if (mask & (1 << person)) continue;
int newMask = mask | (1 << person);
next[newMask] += dp[mask];
if (next[newMask] >= MOD) {
next[newMask] -= MOD;
}
}
}
dp.swap(next);
}
return dp[states - 1];
}
};The copy vector next = dp is the key structural line. It carries every way that ignores the current shirt, then receives additional ways that assign the shirt to one eligible person. Updates read only from dp, the previous shirt layer, so a transition created by shirt 5 cannot be consumed again while shirt 5 is still being processed.