-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0749-contain-virus.js
More file actions
209 lines (181 loc) · 5.74 KB
/
0749-contain-virus.js
File metadata and controls
209 lines (181 loc) · 5.74 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* Contain Virus
* Time Complexity: O((M*N)^2)
* Space Complexity: O(M*N)
*/
var containVirus = function (initialGrid) {
const gridHeight = initialGrid.length;
const gridWidth = initialGrid[0].length;
const upDownWalls = new Set();
const leftRightWalls = new Set();
const moveVectors = [
[-1, 0, -gridWidth, -gridWidth],
[1, 0, gridWidth, 0],
[0, -1, -1, -1],
[0, 1, 1, 0],
];
let totalWallsInstalled = 0;
let currentQuarantineStartRow = -1;
let currentQuarantineStartCol = -1;
function processConnectedCells(
currentProcRow,
currentProcCol,
mapState,
seekingValue,
settingValue,
gatheredThreats,
gatheredVertWalls,
gatheredHorizWalls,
existingUpDownWalls,
existingLeftRightWalls,
) {
if (
currentProcRow < 0 ||
currentProcRow >= gridHeight ||
currentProcCol < 0 ||
currentProcCol >= gridWidth ||
mapState[currentProcRow][currentProcCol] !== seekingValue
) {
return;
}
mapState[currentProcRow][currentProcCol] = settingValue;
const currentCellFlattened = gridWidth * currentProcRow + currentProcCol;
for (let moveDirIndex = 0; moveDirIndex < 4; moveDirIndex++) {
const [rowChange, colChange, threatCellOffset, wallKeyOffset] =
moveVectors[moveDirIndex];
const neighborRow = currentProcRow + rowChange;
const neighborCol = currentProcCol + colChange;
processConnectedCells(
neighborRow,
neighborCol,
mapState,
seekingValue,
settingValue,
gatheredThreats,
gatheredVertWalls,
gatheredHorizWalls,
existingUpDownWalls,
existingLeftRightWalls,
);
if (
neighborRow < 0 ||
neighborRow >= gridHeight ||
neighborCol < 0 ||
neighborCol >= gridWidth
) {
continue;
}
const isVerticalBoundaryCheck = rowChange !== 0;
const wallBoundaryKey = currentCellFlattened + wallKeyOffset;
const isWallAlreadyPresent = isVerticalBoundaryCheck
? existingUpDownWalls.has(wallBoundaryKey)
: existingLeftRightWalls.has(wallBoundaryKey);
if (mapState[neighborRow][neighborCol] !== 0 || isWallAlreadyPresent) {
continue;
}
if (settingValue !== 0) {
gatheredThreats.add(currentCellFlattened + threatCellOffset);
}
if (settingValue === 2) {
if (isVerticalBoundaryCheck) {
gatheredVertWalls.add(wallBoundaryKey);
} else {
gatheredHorizWalls.add(wallBoundaryKey);
}
}
}
}
function findMostThreateningRegion(explorationStartRow, explorationStartCol) {
let iterationThreatenedCells = new Set();
let iterationPotentialVerticalWalls = new Set();
let iterationPotentialHorizontalWalls = new Set();
processConnectedCells(
explorationStartRow,
explorationStartCol,
initialGrid,
1,
2,
iterationThreatenedCells,
iterationPotentialVerticalWalls,
iterationPotentialHorizontalWalls,
upDownWalls,
leftRightWalls,
);
if (iterationThreatenedCells.size > currentMaxThreat) {
currentMaxThreat = iterationThreatenedCells.size;
currentBestVerticalWalls = iterationPotentialVerticalWalls;
currentBestHorizontalWalls = iterationPotentialHorizontalWalls;
currentQuarantineStartRow = explorationStartRow;
currentQuarantineStartCol = explorationStartCol;
}
}
for (let currentSimulationDay = 0; ; currentSimulationDay++) {
let currentMaxThreat = 0;
let currentBestVerticalWalls = new Set();
let currentBestHorizontalWalls = new Set();
currentQuarantineStartRow = -1;
currentQuarantineStartCol = -1;
for (let searchRow = 0; searchRow < gridHeight; searchRow++) {
for (let searchCol = 0; searchCol < gridWidth; searchCol++) {
if (initialGrid[searchRow][searchCol] === 1) {
findMostThreateningRegion(searchRow, searchCol);
}
}
}
if (currentMaxThreat === 0) {
break;
}
currentBestVerticalWalls.forEach((wallIdentifier) =>
upDownWalls.add(wallIdentifier),
);
currentBestHorizontalWalls.forEach((wallIdentifier) =>
leftRightWalls.add(wallIdentifier),
);
totalWallsInstalled +=
currentBestVerticalWalls.size + currentBestHorizontalWalls.size;
let quarantineProcessTarget = 2;
let quarantineProcessSetter = 0;
let dummyThreatAccumulator = new Set();
let dummyVerticalWallsAccumulator = new Set();
let dummyHorizontalWallsAccumulator = new Set();
processConnectedCells(
currentQuarantineStartRow,
currentQuarantineStartCol,
initialGrid,
quarantineProcessTarget,
quarantineProcessSetter,
dummyThreatAccumulator,
dummyVerticalWallsAccumulator,
dummyHorizontalWallsAccumulator,
upDownWalls,
leftRightWalls,
);
let spreadSearchTarget = 1;
let spreadApplyValue = 1;
let newlyInfectedPositions = new Set();
for (let spreadScanRow = 0; spreadScanRow < gridHeight; spreadScanRow++) {
for (let spreadScanCol = 0; spreadScanCol < gridWidth; spreadScanCol++) {
if (initialGrid[spreadScanRow][spreadScanCol] === 1) {
processConnectedCells(
spreadScanRow,
spreadScanCol,
initialGrid,
spreadSearchTarget,
spreadApplyValue,
newlyInfectedPositions,
dummyVerticalWallsAccumulator,
dummyHorizontalWallsAccumulator,
upDownWalls,
leftRightWalls,
);
}
}
}
for (let newInfectionSpot of newlyInfectedPositions) {
initialGrid[Math.floor(newInfectionSpot / gridWidth)][
newInfectionSpot % gridWidth
] = 1;
}
}
return totalWallsInstalled;
};