打卡记录
参加考试的最大学生数(压缩状态DP)
链接
class Solution:
def maxStudents(self, seats: List[List[str]]) -> int:
m, n = len(seats), len(seats[0])
a = [sum((c == '.') << j for j, c in enumerate(s)) for s in seats]
f = [[0] * (1 << n) for _ in range(m)]
for j in range(1, 1 << n):
lb = j & -j
f[0][j] = f[0][j & ~(lb * 3)] + 1
for i in range(1, m):
j = a[i]
while j:
f[i][j] = f[i - 1][a[i - 1]]
s = j
while s:
if (s & (s >> 1)) == 0:
t = a[i - 1] & ~(s << 1 | s >> 1)
f[i][j] = max(f[i][j], f[i - 1][t] + f[0][s])
s = (s - 1) & j
j = (j - 1) & a[i]
f[i][0] = f[i - 1][a[i - 1]]
return f[-1][a[-1]]