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 →Process the men in a fixed order: man 0, then man 1, and so on. Once the first k men have been assigned, the only information that affects the remaining choices is which women are already occupied. The identities of the earlier pairings no longer matter, because every future man only needs to avoid those occupied women.
There are N women, so their occupied set fits in an N-bit integer. Bit j is 1 when woman j is used. A mask with k set bits therefore describes assignments of the first k men. From that state, choose one unset bit belonging to a compatible woman; this assigns the next man and moves to a mask with one more set bit.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n x 2^n) | There are 2^n masks, and each state scans all n women once. The dense matrix can make every scan perform useful work, giving this worst-case bound; incompatible or already-used women only reduce the actual number of updates. |
| Space | O(2^n) extra | The DP array stores one count for each possible woman set, and the input matrix is not counted as working memory. The returned value is a single integer, so there is no output storage to exclude beyond that scalar. The bound is the same for every input shape because the array is allocated for all masks. |
#include <vector>
using namespace std;
class Solution {
public:
int countMatchings(vector<vector<int>> compatibility) {
const int MOD = 1000000007;
int n = static_cast<int>(compatibility.size());
int totalMasks = 1 << n;
vector<int> dp(totalMasks, 0);
dp[0] = 1;
for (int mask = 0; mask < totalMasks; ++mask) {
int man = __builtin_popcount(static_cast<unsigned>(mask));
if (man == n) {
continue;
}
for (int woman = 0; woman < n; ++woman) {
bool unused = (mask & (1 << woman)) == 0;
if (unused && compatibility[man][woman] == 1) {
int nextMask = mask | (1 << woman);
dp[nextMask] += dp[mask];
if (dp[nextMask] >= MOD) {
dp[nextMask] -= MOD;
}
}
}
}
return dp[totalMasks - 1];
}
};The order of masks makes the DP naturally bottom-up. Every transition adds one bit, so it moves from a mask with k set bits to a mask with k + 1 set bits. Consequently, when a state is processed, all assignments that can reach it have already contributed to it. The single subtraction after addition is sufficient because both values are kept below MOD, so their sum is less than 2 x MOD.
A recursive formulation uses the same state meaning but starts at the empty mask and explores only states that can actually be reached through compatible pairs. This can save work when the matrix is sparse or many partial assignments are impossible. Its worst-case time and space remain O(n x 2^n) and O(2^n), and it additionally uses recursion depth O(n), so it is a different arrangement rather than an asymptotic optimisation.
#include <vector>
using namespace std;
class Solution {
public:
int countMatchings(vector<vector<int>> compatibility) {
const int MOD = 1000000007;
n = static_cast<int>(compatibility.size());
matrix = compatibility;
memo.assign(1 << n, -1);
return count(0);
}
private:
int n;
vector<vector<int>> matrix;
vector<int> memo;
int count(int mask) {
int man = __builtin_popcount(static_cast<unsigned>(mask));
if (man == n) {
return 1;
}
if (memo[mask] != -1) {
return memo[mask];
}
long long ways = 0;
for (int woman = 0; woman < n; ++woman) {
bool unused = (mask & (1 << woman)) == 0;
if (unused && matrix[man][woman] == 1) {
ways += count(mask | (1 << woman));
if (ways >= MOD) {
ways -= MOD;
}
}
}
memo[mask] = static_cast<int>(ways);
return memo[mask];
}
};