Leetcode 934. Shortest Bridge
Given an n×n binary grid containing exactly two 4-connected islands, return the minimum number of 0→1 flips needed to connect them. The core challenge is finding the shortest bridge between island boundaries (commonly solved by marking one island and doing a multi-source BFS outward across water until the other island is reached).
Asked at:
Meta
Microsoft
DESCRIPTION
Given an n×n binary grid containing exactly two 4-connected islands, return the minimum number of 0→1 flips needed to connect them. The core challenge is finding the shortest bridge between island boundaries (commonly solved by marking one island and doing a multi-source BFS outward across water until the other island is reached).
Input:
grid = [[0,1],[1,0]]
Output:
1
Explanation: We have island 1 at positions (0,1) and (1,0). They are diagonal neighbors. We can flip either (0,0) or (1,1) to connect them, requiring 1 flip.
Constraints:
- n == grid.length == grid[i].length
- 2 <= n <= 100
- grid[i][j] is either 0 or 1
- There are exactly two 4-connected islands in the grid
Understanding the Problem
Let's understand what we're being asked to do. We have an n×n binary grid (containing only 0s and 1s) with exactly two separate islands. An island is a group of 1s connected horizontally or vertically (4-connected).
For example, given:
[[0,1,0],
[0,0,0],
[0,0,1]]
We have island 1 at position (0,1) and island 2 at position (2,2). They are separated by water (0s).
Our goal: Find the minimum number of 0→1 flips needed to connect these two islands. In the example above, we could flip (1,1) and (1,2) to create a bridge, requiring 2 flips.
Important constraints: The grid contains exactly two 4-connected islands (no more, no less). We need to find the shortest bridge between them.
Edge cases to consider: What if the islands are already adjacent (answer would be 0)? What if the grid is very large? What's the most efficient way to measure distance between island boundaries?
Brute Force Approach
The brute force approach would be to identify all cells belonging to each island using DFS/BFS, then check every pair of cells (one from island 1, one from island 2) and calculate their Manhattan distance. The minimum distance minus 1 would be the answer (since adjacent cells have distance 1 but need 0 flips). This requires iterating through all possible pairs of cells from the two islands.
Start: Find shortest bridge between two islands
0 / 16
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n⁴) We need O(n²) to identify islands, then potentially O(n²) cells in each island, leading to O(n²) × O(n²) = O(n⁴) pairwise distance calculations
Space Complexity: O(n²) We need O(n²) space to store which cells belong to each island, plus recursion stack for DFS
Building Intuition
The key insight is that we need to find the shortest path from any cell of island 1 to any cell of island 2. This is not about connecting specific points - it's about connecting the boundaries of two regions.
Think of it as: "What's the minimum distance between the two island perimeters?" We need to expand outward from one island until we touch the other.
If we tried checking every pair of cells (one from each island), we'd have O(n⁴) comparisons for an n×n grid. That's extremely inefficient!
Instead, by treating all cells of one island as simultaneous starting points and expanding outward level-by-level, we naturally find the shortest bridge. The first time we reach the other island tells us the minimum distance.
Imagine you're standing on island 1 and you have unlimited paint. You paint all cells of island 1 first. Then, in round 1, you paint all water cells adjacent to island 1. In round 2, you paint all water cells adjacent to those. You keep expanding outward in waves.
The moment your paint touches island 2, you know the number of rounds equals the minimum bridge length. This wave-like expansion is exactly what breadth-first search does - it explores all cells at distance d before exploring cells at distance d+1.
Common Mistakes
Optimal Solution
The optimal approach uses a two-phase BFS strategy. First, use DFS or BFS to mark all cells of one island (say, island 1) and add them all to a queue as starting points. Then, perform multi-source BFS from all cells of island 1 simultaneously, expanding outward across water cells level by level. Track the number of levels (distance) as we expand. The moment we reach any cell of island 2, the current level count is our answer - this represents the minimum number of 0→1 flips needed to bridge the islands.
Start: Find shortest bridge between two islands
0 / 71
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n²) We visit each cell at most twice: once during island identification and once during BFS expansion. Since there are n² cells, this gives us O(n²) time complexity
Space Complexity: O(n²) We need O(n²) space for the queue (in worst case, all cells could be in the queue) and O(n²) for tracking visited cells
What We've Learned
- Two-phase BFS strategy: When finding shortest paths between two unknown regions, use DFS/BFS to mark one region first, then multi-source BFS from that region's boundary - this transforms an ambiguous two-target problem into a clear single-target search with known starting points.
- Multi-source BFS for boundary expansion: Instead of starting BFS from a single point, initialize the queue with all boundary cells of the first island - this simultaneously expands in all directions and naturally finds the shortest path to any point on the second island, avoiding the need to try every possible starting point.
- Island marking with DFS: Use DFS to flood-fill the first island you encounter (changing 1s to 2s or adding to a set) - this prevents revisiting the same island during BFS and clearly distinguishes between the two islands, making the bridge-building phase unambiguous.
- Distance tracking without visited array: In BFS for shortest path, track distance as a level counter (incrementing after processing each queue level) rather than storing distance in each cell - this saves space and the queue naturally prevents revisiting cells when you mark them as visited upon addition, not upon removal.
- Early termination on first contact: The BFS can return immediately upon reaching any cell of the second island (a cell with value 1) - because BFS explores layer by layer, the first contact is guaranteed to be the shortest bridge, eliminating the need to explore all possibilities.
- Grid boundary problems pattern: This island-bridging technique applies to any problem requiring minimum connections between separated regions in a grid - think of it for problems involving connecting components, expanding territories, or finding shortest paths between multiple sources and targets in 2D spaces.
Related Concepts and Problems to Practice
medium
This problem uses multi-source BFS on a matrix to spread from multiple starting points simultaneously, which is the exact same pattern as Shortest Bridge where you BFS from one island's boundary to find the other island. Both require marking visited cells and tracking distance/steps.
medium
This problem involves finding shortest distances in a matrix using multi-source BFS, starting from all cells with value 0. Like Shortest Bridge, it requires BFS from multiple sources simultaneously to find minimum distances, making it excellent practice for the same algorithmic pattern.
medium
This problem teaches how to identify and mark connected components (islands) in a matrix, which is the first crucial step in Shortest Bridge. Understanding how to traverse and mark an entire island using DFS/BFS is foundational before attempting to find the shortest bridge between two islands.
Test Your Understanding
Why is matrix the right data structure for this problem?
matrix provides the optimal access pattern
It's the only data structure that works
It's the easiest to implement
It uses the least memory
Select an answer to see the explanation
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Early August, 2025
Microsoft
Senior
Late January, 2025
Meta
Mid-level
Late January, 2025
Meta
Mid-level
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.
Comments
Hello Interview Premium
Your account is free and you can post anonymously if you choose.