Back to course home
0% completed
Vote For New Content
Shortest Bridge (medium)
Problem Statement
You are given a square grid (n x n)
made up of 1
s (land) and 0
s (water). There are exactly two separate islands in the grid.
An island is defined as a group of connected 1
s that are not connected to any other 1
s.
Return the minimum number of 0
s that need to be flipped to connect the two islands into one if you can change any 0
to 1
to connect the two islands.
Examples
Example 1:
- Input: grid =
[[1, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0]]
- Expected Output:
2
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page