MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / buildBridgeBFS

Function buildBridgeBFS

javascript/0934-shortest-bridge.js:36–70  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

34 };
35
36 const buildBridgeBFS = () => {
37 let distance = -1;
38 let currentQueue = [];
39
40 while (queue.length) {
41 currentQueue = queue;
42 queue = [];
43
44 for (let [row, col] of currentQueue) {
45 for (let [dx, dy] of DIRECTIONS) {
46 const nextRow = row + dx;
47 const nextCol = col + dy;
48
49 if (
50 nextRow >= 0 &&
51 nextRow < rows &&
52 nextCol >= 0 &&
53 nextCol < cols &&
54 grid[nextRow][nextCol] !== 2
55 ) {
56 if (grid[nextRow][nextCol] === 1) {
57 return distance + 1;
58 }
59
60 queue.push([nextRow, nextCol]);
61 grid[nextRow][nextCol] = 2;
62 }
63 }
64 }
65
66 distance++;
67 }
68
69 return -1;
70 };
71
72 for (let i = 0; i < rows; i++) {
73 for (let j = 0; j < cols; j++) {

Callers 1

shortestBridgeFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected