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 customer cannot receive a mixture of values, so one frequency count must be reserved for an entire group of customers. For example, if a value appears 7 times, it can serve customers whose total requested quantity is at most 7, but it cannot serve only part of any customer. The order of those customers does not matter; only the subset and its total demand matter.
There are at most 10 customers, so represent the customers already satisfied by a bitmask. Process the distinct values one at a time. For each frequency, either leave it unused or assign it to a nonempty subset of unsatisfied customers whose total demand fits inside that frequency. Because every transition adds only unsatisfied customers, no customer is assigned twice.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(m x 2^m + r x 3^m) | The subset sums take O(m x 2^m). For one frequency, all masks are considered, and the total number of pairs consisting of a mask and one of its submasks is 3^m, so processing all r frequencies costs O(r x 3^m). |
| Space | O(r x 2^m) extra | The table stores one Boolean state for every frequency prefix and customer mask, while sum uses O(2^m) additional space. The answer is a Boolean, so there is no output storage to exclude; the bound is worst when all 50 possible distinct values occur. |
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
public:
bool canDistribute(vector<int>& nums, vector<int>& quantity) {
unordered_map<int, int> freq;
for (int x : nums) {
++freq[x];
}
vector<int> counts;
for (const auto& entry : freq) {
counts.push_back(entry.second);
}
int m = static_cast<int>(quantity.size());
int maskCount = 1 << m;
vector<int> sum(maskCount, 0);
for (int mask = 0; mask < maskCount; ++mask) {
for (int i = 0; i < m; ++i) {
if (mask & (1 << i)) {
sum[mask] += quantity[i];
}
}
}
int r = static_cast<int>(counts.size());
vector<vector<bool>> dp(r + 1, vector<bool>(maskCount, false));
dp[0][0] = true;
for (int i = 0; i < r; ++i) {
for (int mask = 0; mask < maskCount; ++mask) {
if (!dp[i][mask]) {
continue;
}
dp[i + 1][mask] = true;
int remaining = (maskCount - 1) ^ mask;
for (int sub = remaining; sub != 0; sub = (sub - 1) & remaining) {
if (sum[sub] <= counts[i]) {
dp[i + 1][mask | sub] = true;
}
}
}
}
return dp[r][maskCount - 1];
}
};The key transition is the submask loop. remaining contains exactly the customers not represented by mask, so every sub considered there is automatically disjoint from the customers already served. The expression (sub - 1) & remaining visits every nonempty submask of remaining without generating any invalid overlap. The separate carry transition represents choosing not to use the current value at all.