-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path54201Matrix.js
54 lines (51 loc) · 1.51 KB
/
54201Matrix.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
/**
* @param {number[][]} matrix
* @return {number[][]}
*/
var updateMatrix = function(matrix) {
var stack = [];
// create result array
var result = new Array(matrix.length);
var copy = new Array(matrix[0].length).fill(0);
for (var i = 0; i < result.length; i++) {
result[i] = copy.slice();
}
// push 1 values pos to stack.
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] === 1) {
stack.push([i, j]);
}
}
}
var step = 0;
// increase the step, for each pos, if nearby cell
// is 0, then make step and popout this pos from stack
// refresh the matrix for poped out cell.
while(stack.length) {
step++;
var tempStack = [];
var removeMatrixValue = [];
for (var i = 0; i < stack.length; i++) {
var pos = stack[i];
var x = pos[0];
var y = pos[1];
if (x > 0 && matrix[x - 1][y] === 0 ||
x < matrix.length - 1 && matrix[x + 1][y] === 0 ||
y > 0 && matrix[x][y - 1] === 0 ||
y < matrix[0].length - 1 && matrix[x][y + 1] === 0) {
result[x][y] = step;
removeMatrixValue.push(pos);
}
else {
tempStack.push(pos);
}
}
for (var i = 0; i < removeMatrixValue.length; i++) {
matrix[removeMatrixValue[i][0]][removeMatrixValue[i][1]] = 0;
}
stack = tempStack;
}
return result;
};
module.exports = updateMatrix;