Practice Shortest Bridge
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
You are given an n x n binary matrix grid where 1 represents land and 0 represents water. An island is a 4-directionally connected group of 1's not connected to any other 1's. There are exactly two islands in grid. You may change 0's to 1'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.
Source: Shortest Bridge on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
grid = [[0,1],[1,0]]
1
# Example 2
grid = [[0,1,0],[0,0,0],[0,0,1]]
2
# Example 3
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]]
1
Constraints
Related LeetCode Problems
Practice more problems similar to Shortest Bridge.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.