Skip to content

Commit f31c792

Browse files
committed
add JS solution for problem 934
1 parent 4a57286 commit f31c792

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ If you would like to have collaborator permissions on the repo to merge your own
338338
<sub>[0210 - Course Schedule II](https://leetcode.com/problems/course-schedule-ii/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0210-course-schedule-ii.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0210-course-schedule-ii.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0210-course-schedule-ii.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0210-course-schedule-ii.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0210-course-schedule-ii.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0210-course-schedule-ii.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0210-course-schedule-ii.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0210-course-schedule-ii.ts)</div></sub>
339339
<sub>[1462 - Course Schedule IV](https://leetcode.com/problems/course-schedule-iv/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](kotlin%2F1462-course-schedule-iv.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F1462-course-schedule-iv.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
340340
<sub>[1958 - Check if Move Is Legal](https://leetcode.com/problems/check-if-move-is-legal/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F1958-check-if-move-is-legal.cpp)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F1958-check-if-move-is-legal.go)</div></sub> | <sub><div align='center'>[✔️](java%2F1958-check-if-move-is-legal.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F1958-check-if-move-is-legal.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F1958-check-if-move-is-legal.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F1958-check-if-move-is-legal.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
341-
<sub>[0934 - Shortest Bridge](https://leetcode.com/problems/shortest-bridge/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'></div></sub> | <sub><div align='center'>[✔️](kotlin%2F0934-shortest-bridge.kt)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
341+
<sub>[0934 - Shortest Bridge](https://leetcode.com/problems/shortest-bridge/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](javascript%2F0934-shortest-bridge.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0934-shortest-bridge.kt)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
342342
<sub>[1091 - Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](java%2F1091-shortest-path-in-binary-matrix.java)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](kotlin%2F1091-shortest-path-in-binary-matrix.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F1091-shortest-path-in-binary-matrix.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub>
343343
<sub>[0684 - Redundant Connection](https://leetcode.com/problems/redundant-connection/)</sub> | <sub><div align='center'>[✔️](c%2F0684-redundant-connection.c)</div></sub> | <sub><div align='center'>[✔️](cpp%2F0684-redundant-connection.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0684-redundant-connection.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0684-redundant-connection.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0684-redundant-connection.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0684-redundant-connection.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0684-redundant-connection.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0684-redundant-connection.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](rust%2F0684-redundant-connection.rs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](typescript%2F0684-redundant-connection.ts)</div></sub>
344344
<sub>[0323 - Number of Connected Components In An Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/)</sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](cpp%2F0323-number-of-connected-components-in-an-undirected-graph.cpp)</div></sub> | <sub><div align='center'>[✔️](csharp%2F0323-number-of-connected-components-in-an-undirected-graph.cs)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](go%2F0323-number-of-connected-components-in-an-undirected-graph.go)</div></sub> | <sub><div align='center'>[✔️](java%2F0323-number-of-connected-components-in-an-undirected-graph.java)</div></sub> | <sub><div align='center'>[✔️](javascript%2F0323-number-of-connected-components-in-an-undirected-graph.js)</div></sub> | <sub><div align='center'>[✔️](kotlin%2F0323-number-of-connected-components-in-an-undirected-graph.kt)</div></sub> | <sub><div align='center'>[✔️](python%2F0323-number-of-connected-components-in-an-undirected-graph.py)</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>❌</div></sub> | <sub><div align='center'>[✔️](swift%2F0323-number-of-connected-components-in-an-undirected-graph.swift)</div></sub> | <sub><div align='center'>❌</div></sub>

javascript/0934-shortest-bridge.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const DIRECTIONS = [[-1, 0], [1, 0], [0, -1], [0, 1]];
2+
3+
const shortestBridge = (grid) => {
4+
const rows = grid.length;
5+
const cols = grid[0].length;
6+
7+
let queue = [];
8+
9+
const exploreIslandDFS = (row, col) => {
10+
if (row < 0 || row >= rows || col < 0 || col >= cols || grid[row][col] !== 1) {
11+
return false;
12+
}
13+
14+
queue.push([row, col]);
15+
grid[row][col] = 2;
16+
17+
exploreIslandDFS(row - 1, col);
18+
exploreIslandDFS(row + 1, col);
19+
exploreIslandDFS(row, col - 1);
20+
exploreIslandDFS(row, col + 1);
21+
22+
return true;
23+
};
24+
25+
const buildBridgeBFS = () => {
26+
let distance = -1;
27+
let currentQueue = [];
28+
29+
while (queue.length) {
30+
currentQueue = queue;
31+
queue = [];
32+
33+
for (let [row, col] of currentQueue) {
34+
for (let [dx, dy] of DIRECTIONS) {
35+
const nextRow = row + dx;
36+
const nextCol = col + dy;
37+
38+
if (
39+
nextRow >= 0 &&
40+
nextRow < rows &&
41+
nextCol >= 0 &&
42+
nextCol < cols &&
43+
grid[nextRow][nextCol] !== 2
44+
) {
45+
if (grid[nextRow][nextCol] === 1) {
46+
return distance + 1;
47+
}
48+
49+
queue.push([nextRow, nextCol]);
50+
grid[nextRow][nextCol] = 2;
51+
}
52+
}
53+
}
54+
55+
distance++;
56+
}
57+
58+
return -1;
59+
};
60+
61+
for (let i = 0; i < rows; i++) {
62+
for (let j = 0; j < cols; j++) {
63+
if (exploreIslandDFS(i, j)) {
64+
return buildBridgeBFS();
65+
}
66+
}
67+
}
68+
69+
return -1;
70+
};

0 commit comments

Comments
 (0)