1905. Count Sub Islands LeetCode Solution

In this guide, you will get 1905. Count Sub Islands LeetCode Solution with the best time and space complexity. The solution to Count Sub Islands problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Sub Islands solution in C++
  4. Count Sub Islands solution in Java
  5. Count Sub Islands solution in Python
  6. Additional Resources
1905. Count Sub Islands LeetCode Solution image

Problem Statement of Count Sub Islands

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: grid1 = [[1,1,1,0,0],[0,1,1,1,1],[0,0,0,0,0],[1,0,0,0,0],[1,1,0,1,1]], grid2 = [[1,1,1,0,0],[0,0,1,1,1],[0,1,0,0,0],[1,0,1,1,0],[0,1,0,1,0]]
Output: 3
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are three sub-islands.

Example 2:

Input: grid1 = [[1,0,1,0,1],[1,1,1,1,1],[0,0,0,0,0],[1,1,1,1,1],[1,0,1,0,1]], grid2 = [[0,0,0,0,0],[1,1,1,1,1],[0,1,0,1,0],[0,1,0,1,0],[1,0,0,0,1]]
Output: 2
Explanation: In the picture above, the grid on the left is grid1 and the grid on the right is grid2.
The 1s colored red in grid2 are those considered to be part of a sub-island. There are two sub-islands.

See also  958. Check Completeness of a Binary Tree LeetCode Solution

Constraints:

m == grid1.length == grid2.length
n == grid1[i].length == grid2[i].length
1 <= m, n <= 500
grid1[i][j] and grid2[i][j] are either 0 or 1.

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

1905. Count Sub Islands LeetCode Solution in C++

class Solution {
 public:
  int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
    int ans = 0;

    for (int i = 0; i < grid2.size(); ++i)
      for (int j = 0; j < grid2[0].size(); ++j)
        if (grid2[i][j] == 1)
          ans += dfs(grid1, grid2, i, j);

    return ans;
  }

 private:
  int dfs(const vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i,
          int j) {
    if (i < 0 || i == grid1.size() || j < 0 || j == grid2[0].size())
      return 1;
    if (grid2[i][j] != 1)
      return 1;

    grid2[i][j] = 2;  // Mark 2 as visited.

    return dfs(grid1, grid2, i + 1, j) & dfs(grid1, grid2, i - 1, j) &
           dfs(grid1, grid2, i, j + 1) & dfs(grid1, grid2, i, j - 1) &
           grid1[i][j];
  }
};
/* code provided by PROGIEZ */

1905. Count Sub Islands LeetCode Solution in Java

class Solution {
  public int countSubIslands(int[][] grid1, int[][] grid2) {
    int ans = 0;

    for (int i = 0; i < grid2.length; ++i)
      for (int j = 0; j < grid2[0].length; ++j)
        if (grid2[i][j] == 1)
          ans += dfs(grid1, grid2, i, j);

    return ans;
  }

  private int dfs(int[][] grid1, int[][] grid2, int i, int j) {
    if (i < 0 || i == grid1.length || j < 0 || j == grid2[0].length)
      return 1;
    if (grid2[i][j] != 1)
      return 1;

    grid2[i][j] = 2; // Mark 2 as visited.

    return                                                          //
        dfs(grid1, grid2, i + 1, j) & dfs(grid1, grid2, i - 1, j) & //
        dfs(grid1, grid2, i, j + 1) & dfs(grid1, grid2, i, j - 1) & grid1[i][j];
  }
}
// code provided by PROGIEZ

1905. Count Sub Islands LeetCode Solution in Python

class Solution:
  def countSubIslands(
      self,
      grid1: list[list[int]],
      grid2: list[list[int]],
  ) -> int:
    m = len(grid2)
    n = len(grid2[0])

    def dfs(i: int, j: int) -> int:
      if i < 0 or i == m or j < 0 or j == n:
        return 1
      if grid2[i][j] != 1:
        return 1

      grid2[i][j] = 2  # Mark 2 as visited.

      return (dfs(i + 1, j) & dfs(i - 1, j) &
              dfs(i, j + 1) & dfs(i, j - 1) & grid1[i][j])

    ans = 0

    for i in range(m):
      for j in range(n):
        if grid2[i][j] == 1:
          ans += dfs(i, j)

    return ans
# code by PROGIEZ

Additional Resources

See also  2435. Paths in Matrix Whose Sum Is Divisible by K LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.