Skip to content

Commit 4ebae64

Browse files
committed
Runtime: 13 ms (Top 59.97%) | Memory: 53.7 MB (Top 81.02%)
1 parent f4b9875 commit 4ebae64

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

scripts/algorithms/S/Shortest Bridge/Shortest Bridge.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Runtime: 13 ms (Top 59.97%) | Memory: 53.7 MB (Top 81.02%)
2+
13
class Solution {
24
private static int[][] dirs={{1,0},{-1,0},{0,1},{0,-1}};
35
public int shortestBridge(int[][] grid) {
@@ -19,10 +21,10 @@ public int shortestBridge(int[][] grid) {
1921
Pair pair=queue.poll();
2022
for(int k=0;k<4;k++){
2123
int rowDash=pair.row+dirs[k][0];
22-
int colDash=pair.col+dirs[k][1];
23-
if(rowDash<0 || colDash<0 || rowDash>=grid.length || colDash>=grid[0].length ||
24+
int colDash=pair.col+dirs[k][1];
25+
if(rowDash<0 || colDash<0 || rowDash>=grid.length || colDash>=grid[0].length ||
2426
visited[rowDash][colDash]==true )continue;
25-
if(grid[rowDash][colDash]==1) return level;
27+
if(grid[rowDash][colDash]==1) return level;
2628
queue.add(new Pair(rowDash,colDash));
2729
visited[rowDash][colDash]=true;
2830
}
@@ -37,7 +39,7 @@ private void dfs(int[][] grid,int i,int j,LinkedList<Pair> queue,boolean[][] vis
3739
for(int k=0;k<4;k++){
3840
int rowDash=i+dirs[k][0];
3941
int colDash=j+dirs[k][1];
40-
if(rowDash<0 || colDash<0 || rowDash>=grid.length || colDash>=grid[0].length ||
42+
if(rowDash<0 || colDash<0 || rowDash>=grid.length || colDash>=grid[0].length ||
4143
visited[rowDash][colDash]==true || grid[rowDash][colDash]==0)continue;
4244
dfs(grid,rowDash,colDash,queue,visited);
4345
}
@@ -50,4 +52,4 @@ public Pair(int row,int col){
5052
this.col=col;
5153
}
5254
}
53-
}
55+
}

0 commit comments

Comments
 (0)