Skip to content

Commit 2185634

Browse files
authored
Update 1349-maximum-students-taking-exam.js
1 parent f8f214a commit 2185634

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

1349-maximum-students-taking-exam.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,42 @@ function canSit(row, arrangement, newStudent) {
4343
!(arrangement & (newStudent >> 1))
4444
)
4545
}
46+
47+
// another
48+
49+
/**
50+
* @param {character[][]} seats
51+
* @return {number}
52+
*/
53+
const maxStudents = function (seats) {
54+
const m = seats.length
55+
const n = seats[0].length
56+
const validity = []
57+
for (let i = 0; i < m; i++) {
58+
let cur = 0
59+
for (let j = 0; j < n; j++) {
60+
cur = (cur << 1) + (seats[i][j] === '.' ? 1 : 0)
61+
}
62+
validity.push(cur)
63+
}
64+
const f = Array.from({ length: m + 1 }, () => Array(1 << n).fill(-1))
65+
f[0][0] = 0
66+
for (let i = 1; i <= m; i++) {
67+
const valid = validity[i - 1]
68+
for (let j = 0; j < 1 << n; j++) {
69+
if ((j & valid) === j && !(j & (j >> 1))) {
70+
for (let k = 0; k < 1 << n; k++) {
71+
if (!(j & (k >> 1)) && !((j >> 1) & k) && f[i - 1][k] !== -1) {
72+
f[i][j] = Math.max(f[i][j], f[i - 1][k] + bitCount(j))
73+
}
74+
}
75+
}
76+
}
77+
}
78+
return Math.max(...f[m])
79+
}
80+
function bitCount(n) {
81+
const res = n.toString(2).match(/1/g)
82+
return res === null ? 0 : res.length
83+
}
84+

0 commit comments

Comments
 (0)