Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count Sub Islands

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1905" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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: 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. 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.

Explanation

Here's a breakdown of the solution approach and the code:

  • High-Level Approach:

    • Iterate through grid2 and identify islands (connected components of 1s).
    • For each island in grid2, check if it's a "sub-island" by verifying that all cells in that island are also present in grid1. This is done via depth first search (DFS).
    • Count the number of sub-islands found.
  • Complexity:

    • Runtime: O(m * n), where m and n are the dimensions of the grids. We potentially visit each cell in both grids.
    • Storage: O(m * n) in the worst case, due to the recursion depth of the DFS, which can be proportional to the size of the grid if a large island exists.

Code

    def countSubIslands(grid1, grid2):
    """
    Counts the number of sub-islands in grid2.

    Args:
        grid1: The first binary matrix.
        grid2: The second binary matrix.

    Returns:
        The number of sub-islands.
    """

    rows = len(grid1)
    cols = len(grid1[0])
    count = 0

    def is_valid(row, col):
        return 0 <= row < rows and 0 <= col < cols

    def dfs(row, col):
        if not is_valid(row, col) or grid2[row][col] == 0:
            return True  # Considered water or out of bounds

        if grid1[row][col] == 0 and grid2[row][col] == 1:
            return False  # grid2 has land where grid1 has water

        grid2[row][col] = 0  # Mark as visited

        up = dfs(row - 1, col)
        down = dfs(row + 1, col)
        left = dfs(row, col - 1)
        right = dfs(row, col + 1)

        return up and down and left and right

    for r in range(rows):
        for c in range(cols):
            if grid2[r][c] == 1:
                if dfs(r, c):
                    count += 1

    return count

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Count Sub Islands