FastPrepShortest Bridge

Shortest Bridge

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

grid contains exactly two four-directionally connected islands of 1s. Return the minimum number of 0 cells that must be changed to 1 to connect the islands.

Function

shortestBridge(grid: int[][]) → int

Examples

Example 1

grid = [[0,1],[1,0]]return = 1

Flipping either remaining zero joins the islands.

Constraints

  • 2 <= grid.length, grid[0].length <= 100.
  • The grid contains exactly two islands.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int shortestBridge(int[][] grid) {
  // Write your code here.
}
grid[[0,1],[1,0]]
expected1
Checking account…