-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructy-066-bestBridge.js
280 lines (222 loc) · 6.93 KB
/
structy-066-bestBridge.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// https://structy.net/problems/premium/best-bridge
// complexity:
// time O(y * x) because visit each cell once ,
// space(y * x) because of Set()
const bestBridge = (grid) => {
let visited;
// nested for loops: find the first island
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
//// DFS: explore all land "L" in the island
// if (grid[i][j] === "L") {
const isIsland = exploreIsland(grid, i, j, new Set());
// }
if (isIsland.size > 0) {
visited = isIsland;
break;
}
}
}
// BFS: explore distance from island 1 to 2
const wExplored = new Set(visited);
const queue = [];
for (const land of visited) {
const [y, x] = land.split(",");
queue.push([+y, +x, 0]);
}
while (queue.length > 0) {
const [y, x, distance] = queue.shift();
const curYX = y + "," + x;
console.log(y, x, distance);
// console.log(curYX);
if (grid[y][x] === "L" && !visited.has(curYX)) {
// console.log(distance);
return distance - 1;
}
const deltas = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
];
for (const delta of deltas) {
const [neiY, neiX] = delta;
const currentNei = neiY + "," + neiX;
if (isInbound(grid, neiY, neiX) && !wExplored.has(currentNei)) {
wExplored.add(currentNei);
queue.push([neiY, neiX, distance + 1]);
}
}
}
// return visited;
};
const isInbound = (grid, y, x) => {
const boundY = 0 <= y && y < grid.length;
const boundX = 0 <= x && x < grid[0].length;
return boundY && boundX;
};
const exploreIsland = (grid, y, x, visited) => {
if (!isInbound(grid, y, x)) return visited;
if (grid[y][x] === "W") return visited;
const currentL = y + "," + x;
if (visited.has(currentL)) return visited;
visited.add(currentL);
exploreIsland(grid, y + 1, x, visited);
exploreIsland(grid, y - 1, x, visited);
exploreIsland(grid, y, x + 1, visited);
exploreIsland(grid, y, x - 1, visited);
return visited;
};
const grid = [
["W", "W", "W", "L", "L"],
["L", "L", "W", "W", "L"],
["L", "L", "L", "W", "L"],
["W", "L", "W", "W", "W"],
["W", "W", "W", "W", "W"],
["W", "W", "W", "W", "W"],
];
// // -> 1
// const grid = [
// ["W", "W", "W", "W", "W"],
// ["W", "W", "W", "L", "W"],
// ["L", "W", "W", "W", "W"],
// ];
// // -> 3
// const grid = [
// ["W", "L", "W", "W", "W", "W", "W", "W"],
// ["W", "L", "W", "W", "W", "W", "W", "W"],
// ["W", "W", "W", "W", "W", "W", "W", "W"],
// ["W", "W", "W", "W", "W", "W", "W", "W"],
// ["W", "W", "W", "W", "W", "W", "W", "W"],
// ["W", "W", "W", "W", "W", "W", "L", "W"],
// ["W", "W", "W", "W", "W", "W", "L", "L"],
// ["W", "W", "W", "W", "W", "W", "W", "L"],
// ];
// // -> 8
// const grid = [
// ["W", "W", "W", "W", "W"],
// ["W", "W", "W", "W", "W"],
// ["L", "L", "W", "W", "L"],
// ["W", "L", "W", "W", "L"],
// ["W", "W", "W", "L", "L"],
// ["W", "W", "W", "W", "W"],
// ];
// -> 2
console.log(bestBridge(grid));
// const bestBridge = (grid) => {
// // find island: nested for loops
// const visited = new Set();
// const deltas = [
// [-1, 0],
// [1, 0],
// [0, -1],
// [0, 1],
// ];
// // bfs traversal: mark "L" as visited for first island
// let meetW = true;
// for (let i = 0; i < grid.length && meetW; i++) {
// for (let j = 0; j < grid[0].length; j++) {
// if (grid[i][j] === "L") {
// console.log(i, j, grid[i][j]);
// // console.log("as;dlkfjas;dlfj;slkdf");
// const current = i + "," + j;
// !visited.has(current) && visited.add(current);
// const queue = [[i, j]];
// while (queue.length > 0) {
// const [y, x] = queue.shift();
// for (const delta of deltas) {
// const [m, n] = delta;
// const neiY = y + m;
// const neiX = x + n;
// const nei = neiY + "," + neiX;
// if (
// isInbound(grid, neiY, neiX) &&
// grid[neiY][neiX] !== "W" &&
// !visited.has(nei)
// ) {
// queue.push([neiY, neiX]);
// visited.add(nei);
// }
// }
// }
// meetW = false;
// }
// }
// }
// let min = Infinity;
// // loops thru "L" in visited:
// //// get min from all "L"
// visited.forEach(
// (l) => (min = Math.min(min, explore(grid, l, deltas, visited)))
// );
// return visited;
// };
// const isInbound = (grid, y, x) => {
// const boundY = 0 <= y && y < grid.length;
// const boundX = 0 <= x && x < grid[0].length;
// return boundX && boundY;
// };
// const explore = (grid, l, deltas, visited) => {
// const exploreVisited = new Set();
// const [i, j] = l.split(",");
// const queue = [[+i, +j, 0]];
// while (queue.length > 0) {
// // console.log(queue);
// const [y, x, distance] = queue.shift();
// //// bfs traversal from each L until it meets another island's "L"
// for (const delta of deltas) {
// const [m, n] = delta;
// const neiY = y + m;
// const neiX = x + n;
// const nei = neiY + "," + neiX;
// // console.log(neiY, neiX, distance);
// const boundY = 0 <= neiY && neiY < grid.length;
// const boundX = 0 <= neiX && neiX < grid[0].length;
// if (boundY && boundX && !visited.has(nei) && grid[neiY][neiX] === "L") {
// console.log(distance);
// return distance;
// }
// if (boundY && boundX && !visited.has(nei) && !exploreVisited.has(nei)) {
// queue.push([neiY, neiX, distance + 1]);
// exploreVisited.add(nei);
// }
// }
// // console.log("1 circle");
// }
// return Infinity;
// };
// p: grid
// // r: num+
// const bestBridge = (grid) => {
// // find island
// const visited = new Set();
// for (let i = 0; i < grid.length; i++) {
// for (let j = 0; j < grid[0].length; j++) {
// if (grid[i][j] === "L") {
// checkVisited(grid, i, j, visited);
// }
// if (visited.size > 0) {
// const queue = [[i, j, 0]];
// while (queue.length > 0) {
// const [y,x,0] = queue.shift()
// }
// }
// }
// }
// // get all island's 'L'
// // dfs to next island
// return visited;
// };
// // check if inbound
// const isInbound = (grid, y, x) => {
// const boundY = 0 <= y && y < grid.length;
// const boundX = 0 <= x && x < grid[0].length;
// return boundY && boundX;
// };
// // visited set
// const checkVisited = (grid, y, x, visited) => {
// if (!isInbound) return visited;
// const current = y + "," + x;
// visited.add(current);
// return visited;
// };