Maximum Students Taking Exam Problem
Description
LeetCode Problem 1349.
Given a m * n matrix seats that represent seats distributions in a classroom. If a seat is broken, it is denoted by '#' character otherwise it is denoted by a '.' character.
Students can see the answers of those sitting next to the left, right, upper left and upper right, but he cannot see the answers of the student sitting directly in front or behind him. Return the maximum number of students that can take the exam together without any cheating being possible.
Students must be placed in seats in good condition.
Example 1:

Input: seats = [["#",".","#","#",".","#"],
[".","#","#","#","#","."],
["#",".","#","#",".","#"]]
Output: 4
Explanation: Teacher can place 4 students in available seats so they don't cheat on the exam.
Example 2:
Input: seats = [[".","#"],
["#","#"],
["#","."],
["#","#"],
[".","#"]]
Output: 3
Explanation: Place all students in available seats.
Example 3:
Input: seats = [["#",".",".",".","#"],
[".","#",".","#","."],
[".",".","#",".","."],
[".","#",".","#","."],
["#",".",".",".","#"]]
Output: 10
Explanation: Place students in available seats in column 1, 3 and 5.
Constraints:
seatscontains only characters'.'and'#'.m == seats.lengthn == seats[i].length1 <= m <= 81 <= n <= 8
Solution
Approach: Row-by-Row DP over Bitmasks
A student can see the seats to the left, to the right, upper-left and upper-right. Seen from one row, that gives two kinds of rules:
- Inside a row: no two students sit next to each other, and students only sit on good seats.
- Between two neighbouring rows: no student sits diagonally in front of another one.
Both only involve one row, or two rows next to each other, so we can fill the room row by row. With n <= 8 columns, the students in a row fit in a bitmask mask of at most 8 bits (bit j set means a student in column j). Let good be the mask of good seats in the row. A row mask is valid when mask & ~good == 0 (good seats only) and mask & (mask << 1) == 0 (no two adjacent bits).
Let dp[mask] be the most students placed in the rows so far, with the last row filled as mask. For the next row, a valid mask may follow a previous mask prev when mask & (prev << 1) == 0 and mask & (prev >> 1) == 0, meaning nobody sits diagonally behind a student of the previous row. Then
new_dp[mask] = max(dp[prev]) + popcount(mask)
over all compatible prev. Before the first row, only the empty mask exists (dp[0] = 0). The answer is the largest value after the last row.
Sitting directly in front of or behind someone is allowed, so the same column may be used in two neighbouring rows; only the diagonal neighbours are checked between rows.
Complexity: O(m · 4n) time, at most 8 * 256 * 256 steps, and O(2n) space.
class Solution:
def maxStudents(self, seats: List[List[str]]) -> int:
n = len(seats[0])
# dp[mask] = most students in the rows so far, with this row's students at the bits of mask
dp = {0: 0}
for row in seats:
good = sum(1 << j for j, c in enumerate(row) if c == ".")
new_dp = {}
for mask in range(1 << n):
# Only good seats, and nobody directly left or right of someone else.
if mask & ~good or mask & (mask << 1):
continue
count = bin(mask).count("1")
for prev, best in dp.items():
# Nobody upper-left or upper-right of a student in this row.
if mask & (prev << 1) or mask & (prev >> 1):
continue
if best + count > new_dp.get(mask, -1):
new_dp[mask] = best + count
dp = new_dp
return max(dp.values())