-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path1349-maximum-students-taking-exam.js
57 lines (51 loc) · 1.87 KB
/
1349-maximum-students-taking-exam.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* 1349. Maximum Students Taking Exam
* https://leetcode.com/problems/maximum-students-taking-exam/
* Difficulty: Hard
*
* 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.
*/
/**
* @param {character[][]} seats
* @return {number}
*/
var maxStudents = function(seats) {
const rows = seats.length;
const cols = seats[0].length;
const cache = new Map();
return countStudents(0, 0);
function isValid(row, currMask, prevMask) {
for (let j = 0; j < cols; j++) {
if (!(currMask & (1 << j))) continue;
if (seats[row][j] === '#') return false;
if (j > 0 && (currMask & (1 << (j - 1)))) return false;
if (j < cols - 1 && (currMask & (1 << (j + 1)))) return false;
if (row > 0) {
if (j > 0 && (prevMask & (1 << (j - 1)))) return false;
if (j < cols - 1 && (prevMask & (1 << (j + 1)))) return false;
}
}
return true;
}
function countStudents(row, prevMask) {
if (row === rows) return 0;
const key = `${row},${prevMask}`;
if (cache.has(key)) return cache.get(key);
let maxCount = 0;
for (let currMask = 0; currMask < (1 << cols); currMask++) {
if (!isValid(row, currMask, prevMask)) continue;
const students = (currMask.toString(2).match(/1/g) || []).length;
maxCount = Math.max(maxCount, students + countStudents(row + 1, currMask));
}
cache.set(key, maxCount);
return maxCount;
}
};