dp
with dimensions (m+1) x (1<<n)
filled with -1
, where m
is the number of rows and n
is the number of columns in the seats
matrix.dp[0][0]
to 0
as the base case.i
from 1
to m
.mask
states (from 0
to (1<<n) - 1
).mask
is a subset of the valid seats in the current row and there are no adjacent students, proceed to the next step. Otherwise, continue to the next mask
.mask
, iterate over all possible previous row states prev_mask
.prev_mask
is valid and there are no students in the upper left or upper right positions that can lead to cheating, update dp[i][mask]
.dp
array.