Count Servers that Communicate LeetCode solution in JavaScript

1
var countServers = function (grid) {
2
const m = grid.length;
3
const n = grid[0].length;
4

5
const lastRows = [];
6
const lastCols = [];
7

8
const set = new Set();
9

10
for (let i = 0; i < m; i++) {
11
for (let j = 0; j < n; j++) {
12
if (grid[i][j] === 1) {
13
const currIdx = i * n + j;
14

15
const rowNeiIdx = lastRows[i];
16

17
if (rowNeiIdx != null) {
18
set.add(currIdx).add(rowNeiIdx);
19
}
20

21
const colNeiIdx = lastCols[j];
22

23
if (colNeiIdx != null) {
24
set.add(currIdx).add(colNeiIdx);
25
}
26

27
lastRows[i] = currIdx;
28
lastCols[j] = currIdx;
29
}
30
}
31
}
32

33
return set.size;
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0