Advertisement

Shortest Bridge - LeetCode 934 Solution

Shortest Bridge - Complete Solution Guide

Shortest Bridge is LeetCode problem 934, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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 . Example 1: Input: grid = [[0,1],[1,0]] Output: 1 Example 2: Input: grid = [[0,1,0],[0,0,0],[0,0,1]] Output: 2 Example 3: Input:

Detailed Explanation

The problem asks us to find the shortest 'bridge' between two islands in a binary matrix (grid). The grid contains 0s (water) and 1s (land). An island is a group of 4-directionally connected 1s. The goal is to change the minimum number of 0s to 1s to connect the two islands into a single island. The input is an n x n grid of 0s and 1s, with the constraint that there are exactly two separate islands. The output is the minimum number of 0s that need to be flipped to connect the islands.

Solution Approach

The provided solution uses a combination of Depth-First Search (DFS) and Breadth-First Search (BFS). First, DFS is used to find one of the islands and mark it (by changing the '1' values to '2'). Then, BFS is used to find the shortest path from this marked island to the other island. Each level of BFS represents an increment of distance, essentially expanding outward from the first island until the other island is encountered.

Step-by-Step Algorithm

  1. Step 1: Iterate through the grid to find the first '1' (land) cell. This signifies the start of the first island.
  2. Step 2: Use DFS (or BFS) to explore the first island and mark all its cells as '2'. During the DFS/BFS, add these cells to a queue. The queue will be used for the next BFS step.
  3. Step 3: Initialize a 'distance' variable to 0. This represents the number of '0' cells we need to flip.
  4. Step 4: Perform BFS starting from the queue (containing the cells of the first island).
  5. Step 5: In each BFS level, iterate through all cells in the queue.
  6. Step 6: For each cell, explore its 4-directional neighbors.
  7. Step 7: If a neighbor is '1' (part of the second island), return the current 'distance'.
  8. Step 8: If a neighbor is '0' (water), change it to '2' (visited) and add it to the queue. This expands the search outwards from the first island.
  9. Step 9: Increment the 'distance' after each BFS level. This represents the number of '0' cells crossed.
  10. Step 10: If the BFS completes without finding the second island, return -1 (although this should not happen given the problem constraints).

Key Insights

  • Insight 1: Identify one island using Depth-First Search (DFS) or Breadth-First Search (BFS) and mark all its cells.
  • Insight 2: Use BFS to expand from the identified island, layer by layer, until the second island is reached. The number of layers traversed represents the shortest bridge length.
  • Insight 3: The problem can be solved by first marking one of the islands and then finding the shortest path (in terms of 0s crossed) to the other island, using the first island as the starting point.

Complexity Analysis

Time Complexity: O(n^2)

Space Complexity: O(n^2)

Topics

This problem involves: Array, Depth-First Search, Breadth-First Search, Matrix.

Companies

Asked at: Coupang, Docusign, Flipkart, McKinsey, Snap.