3
int dp[8][(1 << 8) - 1];4
int maxStudents(vector<vector<char>> &seats) {5
memset(dp, -1, sizeof(dp));6
return helper(seats, 0, 0);8
int helper(vector<vector<char>> &seats, int prevAllocation, int idx) {9
if (idx == seats.size()) return 0;10
int sz = seats[0].size(), masks = ((1 << sz) - 1), maxi = 0;11
// masks is for iterating over all subsets12
if (dp[idx][prevAllocation] != -1) return dp[idx][prevAllocation];14
int curAllocation = 0, cnt = 0;15
for (int i = 0; i < sz; i++) {16
if (seats[idx][i] != '#' && masks >> i & 1) {17
int ul = i - 1, ur = i + 1;20
if (prevAllocation >> ul & 1 || curAllocation >> ul & 1) canPlace = false;22
if (prevAllocation >> ur & 1 || curAllocation >> ur & 1) canPlace = false;24
curAllocation |= 1 << i;29
maxi = max(maxi, cnt + helper(seats, curAllocation, idx + 1));32
dp[idx][prevAllocation] = maxi;