Shortest Bridge
Try to solve the Shortest Bridge problem.
We'll cover the following
Statement
We are given an binary matrix grid containing 0
s and 1
s. Each cell in the grid represents either land or water. A cell with a value of 1
represents land, while a cell with a value of 0
represents water. A group of adjacent cells with a value of 1
constitutes an island. Two cells are considered adjacent if one is above, below, to the left, or to the right of the other. Our task is to return the smallest number of 0
s we must flip to connect the two islands.
Note: We may assume all four edges of the grid are surrounded by water.
Constraints:
-
n
-
grid.length
grid[i].length
. -
grid[i][j]
is either0
or1
. -
There are exactly two islands in the
grid
.
Example
Create a free account to view this lesson.
By signing up, you agree to Educative's Terms of Service and Privacy Policy