Advertisement

Count Sub Islands - LeetCode 1905 Solution

Count Sub Islands - Complete Solution Guide

Count Sub Islands is LeetCode problem 1905, 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 two m x n binary matrices grid1 and grid2 containing only 0 's (representing water) and 1 's (representing land). An island is a group of 1 's connected 4-directionally (horizontal or vertical). Any cells outside of the grid are considered water cells. An island in grid2 is considered a sub-island if there is an island in grid1 that contains all the cells that make up this island in grid2 . Return the number of islands in grid2 that are considered sub-islands . Example 1: Input: gr

Detailed Explanation

The problem asks us to find the number of "sub-islands" present in `grid2`. A sub-island is an island in `grid2` that is also entirely contained within an island in `grid1`. Both grids contain only 0s (water) and 1s (land), and an island is a group of 1s connected horizontally or vertically (4-directionally). The goal is to count how many islands in `grid2` are sub-islands of islands in `grid1`.

Solution Approach

The solution first eliminates all islands in `grid2` that have at least one cell that corresponds to a water cell in `grid1`. Then, it counts the remaining islands in `grid2`. Each remaining island is guaranteed to be a sub-island since we've removed any island in `grid2` that isn't fully contained within an island in `grid1`. We use a `sink` function (implemented using a stack-based DFS) to set all cells of an island to 0. The `sink` function is called when an island in `grid2` has a corresponding 0 in `grid1`. Finally, we iterate through the modified `grid2` and count the remaining islands, sinking them as we go to avoid overcounting.

Step-by-Step Algorithm

  1. Step 1: Iterate through each cell in `grid2`.
  2. Step 2: If a cell in `grid2` is land (1) AND the corresponding cell in `grid1` is water (0), then call the `sink` function to eliminate the entire island connected to that cell in `grid2`.
  3. Step 3: After eliminating the non-sub-islands in `grid2`, iterate through `grid2` again.
  4. Step 4: If a cell in the modified `grid2` is land (1), then it represents a sub-island. Increment the sub-island count and call `sink` to eliminate this island to avoid counting it again.
  5. Step 5: Return the final sub-island count.

Key Insights

  • Insight 1: Islands in `grid2` that have a corresponding land cell in `grid1` as water are *not* sub-islands. We can effectively eliminate these islands by 'sinking' them in `grid2`.
  • Insight 2: After sinking the non-sub-island components, we simply count the remaining islands in `grid2`. Each remaining island is guaranteed to be a sub-island.
  • Insight 3: Depth-First Search (DFS) or Breadth-First Search (BFS) is an efficient way to identify and sink connected components (islands) in the grids.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

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

Companies

Asked at: DoorDash, X, Zepto.