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 →A valid assignment gives every person one preferred hat, while no hat can belong to two people. If you process hats one by one, the only history that matters before considering the next hat is which people already have hats. The exact hat choices used earlier do not need separate storage, because the processing order itself remembers that each earlier hat has been handled.
Represent the assigned people with a bitmask. Bit p is 1 when person p already wears a hat, and 0 otherwise. For each hat, you either leave it unused or give it to one unassigned person who prefers it. Those choices cover every assignment exactly once: the assignment is reconstructed by deciding what happened when each of its hats was processed.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(40 x 2^n x n) | For each of the 40 hats, every one of the 2^n masks is visited, and the inner loop examines at most n people who prefer that hat. Each hat-mask-person combination is processed once, so no assignment prefix is expanded more than once for that transition. |
| Space | O(2^n + 40n) extra, which is O(2^n) with 40 fixed | The DP stores one value for each people-mask, and the hat-to-person buckets store at most 40n preference entries. The returned count is a scalar, so there is no output array to include; in the worst input shape, every person prefers every hat and the bucket term reaches 40n. |
#include <vector>
using namespace std;
class Solution {
public:
int numberWays(vector<vector<int>>& hats) {
const int MOD = 1000000007;
int n = hats.size();
vector<int> hatToPerson[41];
for (int i = 0; i < n; ++i) {
for (int hat : hats[i]) {
hatToPerson[hat].push_back(i);
}
}
vector<int> dp(1 << n, 0);
dp[0] = 1;
for (int hat = 1; hat <= 40; ++hat) {
for (int mask = (1 << n) - 1; mask >= 0; --mask) {
for (int person : hatToPerson[hat]) {
if ((mask & (1 << person)) == 0) {
int newMask = mask | (1 << person);
dp[newMask] = (dp[newMask] + dp[mask]) % MOD;
}
}
}
}
return dp[(1 << n) - 1];
}
};You can avoid reasoning about scan direction by keeping dp for the hats processed so far and writing every result for the current hat into next. Start next as a copy of dp to represent skipping the hat, then add assignment transitions from dp only. This is merely a different arrangement, not an asymptotic optimisation: it uses O(2^n) extra storage and makes the per-hat dependency explicit.
#include <vector>
using namespace std;
class Solution {
public:
int numberWays(vector<vector<int>>& hats) {
const int MOD = 1000000007;
int n = hats.size();
vector<int> hatToPerson[41];
for (int person = 0; person < n; ++person) {
for (int hat : hats[person]) {
hatToPerson[hat].push_back(person);
}
}
vector<int> dp(1 << n, 0);
dp[0] = 1;
for (int hat = 1; hat <= 40; ++hat) {
vector<int> next = dp;
for (int mask = 0; mask < (1 << n); ++mask) {
for (int person : hatToPerson[hat]) {
if ((mask & (1 << person)) == 0) {
int newMask = mask | (1 << person);
next[newMask] = (next[newMask] + dp[mask]) % MOD;
}
}
}
dp.swap(next);
}
return dp[(1 << n) - 1];
}
};