Package g0901_1000.s0934_shortest_bridge
Class Solution
- java.lang.Object
-
- g0901_1000.s0934_shortest_bridge.Solution
-
public class Solution extends Object
934 - Shortest Bridge.Medium
You are given an
n x nbinary matrixgridwhere1represents land and0represents water.An island is a 4-directionally connected group of
1’s not connected to any other1’s. There are exactly two islands ingrid.You may change
0’s to1’s to connect the two islands to form one island.Return the smallest number of
0’s you must flip to connect the two islands.Example 1:
Input: grid = [[0,1],[1,0]]
Output: 1
Example 2:
Input: grid = [[0,1,0],[0,0,0],[0,0,1]]
Output: 2
Example 3:
Input: grid = [[1,1,1,1,1],[1,0,0,0,1],[1,0,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]
Output: 1
Constraints:
n == grid.length == grid[i].length2 <= n <= 100grid[i][j]is either0or1.- There are exactly two islands in
grid.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intshortestBridge(int[][] grid)
-