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 grid from left to right, then move to the next row. When you are deciding a cell, every cell above it and every cell to its left has already been decided, while every cell to its right and below is still unknown. Therefore, the new person's contribution depends only on the state above and the state immediately to the left.
The whole previous row is more information than the current cell needs at once, but you must preserve it because each column has a different cell above it. Encode every cell in that row as 0 for empty, 1 for introvert, or 2 for extrovert. A row of n cells becomes one base-3 number, so the mask has only 3^n possible values. Along with the mask, keep how many introverts and extroverts remain.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(m x n x I x E x S) | There is one memoized state for each row, column, pair of remaining counts, and mask. Each state checks at most three placements and does constant work, so no state is expanded twice. |
| Space | O(m x n x I x E x S) extra | The memo table stores one integer per state. The recursion stack adds O(m x n), which is smaller than the table for the allowed dimensions. The returned happiness is one integer, so there is no output array to count; the bound is worst when m, n, I, and E are all as large as permitted, and the exponential growth comes from S = 3^n. |
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class Solution {
public:
int getMaxGridHappiness(int m, int n, int introvertsCount, int extrovertsCount) {
vector<int> power3(n + 1, 1);
for (int i = 1; i <= n; ++i) {
power3[i] = power3[i - 1] * 3;
}
int maskCount = power3[n];
vector<vector<vector<vector<vector<int>>>>> memo(
m,
vector<vector<vector<vector<int>>>>(
n,
vector<vector<vector<int>>>(
introvertsCount + 1,
vector<vector<int>>(
extrovertsCount + 1,
vector<int>(maskCount, -1)))));
function<int(int, int, int, int, int)> dfs =
[&](int row, int col, int introverts, int extroverts, int mask) -> int {
if (row == m) {
return 0;
}
if (col == n) {
return dfs(row + 1, 0, introverts, extroverts, mask);
}
int& answer = memo[row][col][introverts][extroverts][mask];
if (answer != -1) {
return answer;
}
int current = (mask / power3[col]) % 3;
int up = (row == 0) ? 0 : current;
int left = (col == 0) ? 0 : (mask / power3[col - 1]) % 3;
int emptyMask = mask - current * power3[col];
answer = dfs(row, col + 1, introverts, extroverts, emptyMask);
if (introverts > 0) {
int gain = 120;
if (up != 0) gain -= 30;
if (left != 0) gain -= 30;
int introvertMask = emptyMask + power3[col];
answer = max(answer,
gain + dfs(row, col + 1, introverts - 1,
extroverts, introvertMask));
}
if (extroverts > 0) {
int gain = 40;
if (up != 0) gain += 20;
if (left != 0) gain += 20;
int extrovertMask = emptyMask + 2 * power3[col];
answer = max(answer,
gain + dfs(row, col + 1, introverts,
extroverts - 1, extrovertMask));
}
return answer;
};
return dfs(0, 0, introvertsCount, extrovertsCount, 0);
}
};The mask update is the line worth remembering. First remove the old digit at the current column, then add the digit for the chosen state. This matters even for the empty branch: without clearing that digit, an unoccupied cell in the current row incorrectly copies the occupant that stood there in the previous row.