-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathShortest Bridge.js
58 lines (56 loc) · 1.74 KB
/
Shortest Bridge.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
// Runtime: 169 ms (Top 64.28%) | Memory: 49.7 MB (Top 82.47%)
/**
* @param {number[][]} grid
* @return {number}
*/
const DIR = [
[0,1],
[0,-1],
[1,0],
[-1,0]
];
var shortestBridge = function(grid) {
const que = [];
const ROWS = grid.length;
const COLS = grid[0].length;
// find first insland
outer:
for(let row=0; row<ROWS; row++) {
for(let col=0; col<COLS; col++) {
if(grid[row][col] == 1) {
const stack = [[row, col]];
while(stack.length) {
const [r, c] = stack.pop();
que.push([r, c]);
grid[r][c] = 2; // mark as visited.
for(const dir of DIR) {
const newRow = r+dir[0];
const newCol = c+dir[1];
if(newRow<0 || newCol<0 || newRow>=ROWS || newCol>=COLS) continue;
if(grid[newRow][newCol] != 1) continue;
stack.push([newRow, newCol]);
}
}
break outer;
}
}
}
let steps = 0;
while(que.length) {
let size = que.length;
for(let i=0; i<size; i++) {
const [row, col] = que.shift();
for(const dir of DIR) {
const newRow = row+dir[0];
const newCol = col+dir[1];
if(newRow<0 || newCol<0 || newRow>=ROWS || newCol>=COLS) continue;
if(grid[newRow][newCol] == 2) continue;
if(grid[newRow][newCol] == 1) return steps;
grid[newRow][newCol] = 2; // mark as visited.
que.push([newRow, newCol]);
}
}
steps++;
}
return -1;
};