forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1349.js
37 lines (34 loc) · 1.12 KB
/
1349.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
var maxStudents = function(seats) {
let r = seats.length, c = seats[0].length;
let d = [[-1, -1], [0, -1], [1, -1], [-1, 1], [0, 1], [1, 1]];
let match = new Array(r * c);
let res = 0, cnt = 0;
let vis = new Array(r * c);
let find = function(node, seats) {
for (let it of d) {
let nx = it[0] + node[0], ny = it[1] + node[1];
if (nx >= 0 && nx < r && ny >= 0&& ny < c && vis[nx * c + ny] == 0 && seats[nx][ny] == '.') {
vis[nx * c + ny] = 1;
if (match[nx * c + ny] == null || find(match[nx * c + ny], seats)) {
match[nx * c + ny] = node;
return true;
}
}
}
return false;
}
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j += 2) {
if (seats[i][j] != '.') continue;
vis.fill(0);
let node = [i, j];
if (find(node, seats)) res++;
}
}
for (let i = 0; i < r; i++) {
for (let j = 0; j < c; j++){
if (seats[i][j] == '.') cnt++;
}
}
return cnt - res;
};