-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
49 lines (40 loc) · 1.06 KB
/
main.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
// URL: https://leetcode.com/problems/01-matrix/
/**
* @param {number[][]} mat
* @return {number[][]}
*/
const updateMatrix = (matrix) => {
const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
];
const N = matrix.length;
const M = matrix[0].length;
const output = new Array(N).fill().map(() => Array(M).fill(0));
const queue = [];
for (let r = 0; r < matrix.length; r++) {
for (let c = 0; c < matrix[0].length; c++) {
if (matrix[r][c] === 0) queue.push([r, c]);
else output[r][c] = -1;
}
}
while (queue.length) {
const len = queue.length;
for (let i = 0; i < len; i++) {
const [row, col] = queue.shift();
for (const [dx, dy] of directions) {
const x = row + dx;
const y = col + dy;
if (x < 0 || x >= matrix.length) continue;
if (y < 0 || y >= matrix[0].length) continue;
if (matrix[x][y] === 0) continue;
if (output[x][y] !== -1) continue;
output[x][y] = output[row][col] + 1;
queue.push([x, y]);
}
}
}
return output;
};