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 →Students in the same row can cheat only with the student immediately to their left or right, so two occupied neighboring seats are forbidden. Students in adjacent rows can cheat diagonally, so a student in column j conflicts with students in columns j - 1 and j + 1 in the row above. No student can see beyond the previous row, which is the key reason a row-by-row dynamic program works.
Encode one row with a bitmask: bit j is 1 when column j contains a student. A mask is valid for its own row when every 1 lies on a good seat and no two 1 bits are adjacent. Given the previous row's mask, the current mask is also valid only when their 1 bits do not touch diagonally. Once the current mask is chosen, earlier rows matter only through the best score already stored for this previous mask.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(m x 4^n) | There are m x 2^n memoized states, and each state scans all 2^n current masks. The validity checks and popcount take constant time for fixed machine-word masks, so the scan dominates; this bound is reached when most masks remain possible. |
| Space | O(m x 2^n) extra | The memo table stores one value for every row and previous mask, while rowMask and the recursion stack use only O(m) more space. The returned integer is not output storage, and no large output structure exists here; the extra-space bound is largest when all 2^n previous masks are represented for every row. |
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class Solution {
public:
int maxStudents(vector<vector<char>>& seats) {
int m = static_cast<int>(seats.size());
int n = static_cast<int>(seats[0].size());
vector<int> rowMask(m, 0);
for (int row = 0; row < m; ++row) {
for (int col = 0; col < n; ++col) {
if (seats[row][col] == '.') {
rowMask[row] |= (1 << col);
}
}
}
int maskCount = 1 << n;
vector<vector<int>> memo(m, vector<int>(maskCount, -1));
function<int(int, int)> solve = [&](int row, int prev) -> int {
if (row == m) {
return 0;
}
if (memo[row][prev] != -1) {
return memo[row][prev];
}
int best = 0;
for (int mask = 0; mask < maskCount; ++mask) {
if ((mask & rowMask[row]) != mask) {
continue;
}
if ((mask & (mask << 1)) != 0) {
continue;
}
if ((mask & (prev << 1)) != 0) {
continue;
}
if ((mask & (prev >> 1)) != 0) {
continue;
}
int students = __builtin_popcount(static_cast<unsigned>(mask));
best = max(best, students + solve(row + 1, mask));
}
memo[row][prev] = best;
return best;
};
return solve(0, 0);
}
};The expression mask & (mask << 1) detects adjacent 1 bits inside the current row. Shifting prev left and right aligns the previous row's students with the current row positions that would be diagonally visible. The initial previous mask is zero because there is no row above the first row, and the base case returns zero after the last row contributes nothing.
The memoized solution is natural when you want each state to describe a suffix of rows. You can also process rows from top to bottom and keep only the best score for each mask in the immediately previous row. After computing the next array, discard the old one. This is a genuine space optimisation, not a different recurrence: every transition and every validity test stays the same.
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
int maxStudents(vector<vector<char>>& seats) {
int m = static_cast<int>(seats.size());
int n = static_cast<int>(seats[0].size());
int maskCount = 1 << n;
vector<int> rowMask(m, 0);
for (int row = 0; row < m; ++row) {
for (int col = 0; col < n; ++col) {
if (seats[row][col] == '.') {
rowMask[row] |= (1 << col);
}
}
}
vector<int> previous(maskCount, -1);
previous[0] = 0;
for (int row = 0; row < m; ++row) {
vector<int> current(maskCount, -1);
for (int prev = 0; prev < maskCount; ++prev) {
if (previous[prev] == -1) {
continue;
}
for (int mask = 0; mask < maskCount; ++mask) {
if ((mask & rowMask[row]) != mask) {
continue;
}
if ((mask & (mask << 1)) != 0) {
continue;
}
if ((mask & (prev << 1)) != 0) {
continue;
}
if ((mask & (prev >> 1)) != 0) {
continue;
}
int students = __builtin_popcount(static_cast<unsigned>(mask));
current[mask] = max(current[mask], previous[prev] + students);
}
}
previous.swap(current);
}
return *max_element(previous.begin(), previous.end());
}
};The rolling version reduces extra space from O(m x 2^n) to O(2^n), while time remains O(m x 4^n) because it still considers every previous and current mask pair. It is useful when memory matters or when you prefer an iterative table, but the top-down form makes the meaning of each state and the terminal row especially clear.