-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0803-bricks-falling-when-hit.js
More file actions
174 lines (158 loc) · 3.82 KB
/
0803-bricks-falling-when-hit.js
File metadata and controls
174 lines (158 loc) · 3.82 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Bricks Falling When Hit
* Time Complexity: O(R * C + H)
* Space Complexity: O(R * C + H)
*/
var hitBricks = function (grid, hits) {
const gridRows = grid.length;
const gridCols = grid[0].length;
const movesArray = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
];
const fallCounts = new Array(hits.length).fill(0);
let hitIdx = 0;
while (hitIdx < hits.length) {
const currentHitCoord = hits[hitIdx];
const hitGridRow = currentHitCoord[0];
const hitGridCol = currentHitCoord[1];
if (grid[hitGridRow][hitGridCol] === 1) {
grid[hitGridRow][hitGridCol] = 2;
}
hitIdx++;
}
let columnIterator = 0;
while (columnIterator < gridCols) {
markInitialStableBricks(
0,
columnIterator,
grid,
gridRows,
gridCols,
movesArray,
);
columnIterator++;
}
let revertHitIndex = hits.length - 1;
while (revertHitIndex >= 0) {
const currentImpactCoords = hits[revertHitIndex];
const impactRow = currentImpactCoords[0];
const impactCol = currentImpactCoords[1];
if (grid[impactRow][impactCol] !== 2) {
revertHitIndex--;
continue;
}
grid[impactRow][impactCol] = 1;
if (
isBrickStable(impactRow, impactCol, grid, gridRows, gridCols, movesArray)
) {
const newlyStabilizedBricks = countConnectedStable(
impactRow,
impactCol,
grid,
gridRows,
gridCols,
movesArray,
);
fallCounts[revertHitIndex] = newlyStabilizedBricks - 1;
}
revertHitIndex--;
}
return fallCounts;
function markInitialStableBricks(
rowPosition,
colPosition,
currentGridState,
totalRows,
totalCols,
directionsSet,
) {
if (
rowPosition < 0 ||
rowPosition >= totalRows ||
colPosition < 0 ||
colPosition >= totalCols ||
currentGridState[rowPosition][colPosition] !== 1
) {
return 0;
}
currentGridState[rowPosition][colPosition] = 3;
let componentSize = 1;
for (let directionVector of directionsSet) {
const nextRowPos = rowPosition + directionVector[0];
const nextColPos = colPosition + directionVector[1];
componentSize += markInitialStableBricks(
nextRowPos,
nextColPos,
currentGridState,
totalRows,
totalCols,
directionsSet,
);
}
return componentSize;
}
function isBrickStable(
checkRow,
checkCol,
currentGridState,
totalRows,
totalCols,
directionsSet,
) {
if (checkRow === 0) {
return true;
}
for (let searchDirection of directionsSet) {
const neighbourRow = checkRow + searchDirection[0];
const neighbourCol = checkCol + searchDirection[1];
if (
neighbourRow >= 0 &&
neighbourRow < totalRows &&
neighbourCol >= 0 &&
neighbourCol < totalCols &&
currentGridState[neighbourRow][neighbourCol] === 3
) {
return true;
}
}
return false;
}
function countConnectedStable(
startRow,
startCol,
currentGridState,
totalRows,
totalCols,
directionsSet,
) {
if (
startRow < 0 ||
startRow >= totalRows ||
startCol < 0 ||
startCol >= totalCols ||
currentGridState[startRow][startCol] !== 1
) {
return 0;
}
currentGridState[startRow][startCol] = 3;
let stableBrickCount = 1;
let directionIndex = 0;
while (directionIndex < directionsSet.length) {
const adjacentRow = startRow + directionsSet[directionIndex][0];
const adjacentCol = startCol + directionsSet[directionIndex][1];
stableBrickCount += countConnectedStable(
adjacentRow,
adjacentCol,
currentGridState,
totalRows,
totalCols,
directionsSet,
);
directionIndex++;
}
return stableBrickCount;
}
};