-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
38 lines (38 loc) · 1.21 KB
/
Solution.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution {
public:
int maxStudents(vector<vector<char>>& seats) {
int m = seats.size();
int n = seats[0].size();
vector<int> ss(m);
vector<vector<int>> f(1 << n, vector<int>(m, -1));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (seats[i][j] == '.') {
ss[i] |= 1 << j;
}
}
}
function<int(int, int)> dfs = [&](int seat, int i) -> int {
if (f[seat][i] != -1) {
return f[seat][i];
}
int ans = 0;
for (int mask = 0; mask < 1 << n; ++mask) {
if ((seat | mask) != seat || (mask & (mask << 1)) != 0) {
continue;
}
int cnt = __builtin_popcount(mask);
if (i == m - 1) {
ans = max(ans, cnt);
} else {
int nxt = ss[i + 1];
nxt &= ~(mask >> 1);
nxt &= ~(mask << 1);
ans = max(ans, cnt + dfs(nxt, i + 1));
}
}
return f[seat][i] = ans;
};
return dfs(ss[0], 0);
}
};