Bricks Falling When Hit LeetCode solution in JavaScript

1
var hitBricks = function (grid, hits) {
2
let output = [];
3
for (let i = 0; i < hits.length; i++) {
4
let map = {};
5

6
if (grid[hits[i][0]][hits[i][1]] == 1) {
7
grid[hits[i][0]][hits[i][1]] = 0;
8

9
for (let j = 0; j < grid[0].length; j++) {
10
if (grid[0][j] == 1) {
11
dfs(grid, output, map, 0, j);
12
break;
13
}
14
}
15
/* removing bricks that are not connected and adding the count to array */
16
removeBricks(grid, map, output);
17
} else {
18
output.push(0);
19
}
20
}
21
return output;
22
};
23

24
function dfs(grid, output, map, i, j) {
25
if (i >= grid.length || j >= grid[0].length || i < 0 || j < 0) return;
26

27
let key = i + "_" + j;
28

29
if (map[key]) return;
30

31
if (grid[i][j] == 1) {
32
map[key] = 1;
33

34
dfs(grid, output, map, i + 1, j);
35
dfs(grid, output, map, i - 1, j);
36
dfs(grid, output, map, i, j + 1);
37
dfs(grid, output, map, i, j - 1);
38
}
39
}
40

41
function removeBricks(grid, map, output) {
42
let count = 0;
43
for (let row = 0; row < grid.length; row++) {
44
for (let col = 0; col < grid[row].length; col++) {
45
let key = row + "_" + col;
46

47
if (grid[row][col] == 1 && !map[key]) {
48
grid[row][col] = 0;
49
count++;
50
}
51
}
52
}
53
output.push(count);
54
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0