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 group contributes independently of the groups around it: once you know which items are together, its contribution is simply the sum of a[i][j] over pairs inside that group. So for every subset, you can first compute the score of putting that entire subset into one group. The hard part is not scoring one group; it is choosing which groups form the partition.
Let dp[mask] be the best score for partitioning exactly the items whose bits are set in mask. Choose the lowest-indexed item still present. Its group must be some submask sub of mask that contains this item. The remaining items are mask without sub, so the transition is groupScore[sub] + dp[mask ^ sub]. Every partition has exactly one group containing the chosen item, which makes this choice complete without counting the same groups in different orders.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(n * 2^n + 3^n) | Each groupScore state scans at most n possible partners, giving O(n * 2^n). Across all masks, enumerating submasks gives O(3^n): each item has three states, outside the mask, inside the mask but outside the chosen submask, or inside the chosen submask. The first-bit restriction only removes duplicate group orders, so it cannot increase this bound. |
| Space | O(2^n) extra | The groupScore and dp arrays each contain one value per mask, and the remaining variables use O(1) space. The returned value is a scalar and output storage is excluded; the extra memory is still O(2^n), which is the worst shape for every allowed n. |
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
class Solution {
public:
long long maxScore(vector<vector<int>> a) {
int n = static_cast<int>(a.size());
int total = 1 << n;
vector<long long> groupScore(total, 0);
vector<long long> dp(total, 0);
for (int mask = 1; mask < total; ++mask) {
int bit = __builtin_ctz(static_cast<unsigned int>(mask));
int rest = mask ^ (1 << bit);
groupScore[mask] = groupScore[rest];
for (int j = 0; j < n; ++j) {
if (rest & (1 << j)) {
groupScore[mask] += a[bit][j];
}
}
}
for (int mask = 1; mask < total; ++mask) {
int first = mask & -mask;
long long best = LLONG_MIN / 4;
for (int sub = mask; sub; sub = (sub - 1) & mask) {
if (sub & first) {
best = max(best, groupScore[sub] + dp[mask ^ sub]);
}
}
dp[mask] = best;
}
return dp[total - 1];
}
};The recurrence for groupScore is the first key detail. Removing bit from mask leaves rest, whose score is already known. Adding bit back creates exactly the pairs between bit and items in rest, so the loop adds each unordered pair once. The diagonal is never included because bit is not part of rest, and the matrix's symmetry means the chosen direction gives the same score.
The second key detail is first = mask & -mask. It identifies one fixed item in the remaining set. Every valid partition has exactly one group containing that item, so considering only submasks that contain first still considers every partition. It also gives each partition one construction order instead of repeatedly rediscovering it with its groups permuted.