Count Sub Islands: BFS

Problem can be solved with either a DFS or BFS, decided to go with BFS:

1/ Parse grid2 looking for islands. Use BFS to find them. Store them (O(N))

2/ Go thru the list of islands from #1, check whether they exist in grid1 (O(N))

Code is below, happy Father's Day!!! ACC

Count Sub Islands - LeetCode

1905. Count Sub Islands
Medium

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.
Accepted
5,574
Submissions
9,590

public class IslandCell
{
    public int row = 0;
    public int col = 0;

    public IslandCell(int row, int col)
    {
        this.row = row;
        this.col = col;
    }
}

public class Solution {
    public int CountSubIslands(int[][] grid1, int[][] grid2)
    {
        List> islands = new List>();

        for (int r = 0; r < grid2.Length; r++)
        {
            for (int c = 0; c < grid2[r].Length; c++)
            {
                if (grid2[r][c] == 1)
                {
                    List island = new List();

                    Queue queue = new Queue();
                    grid2[r][c] = 0;
                    queue.Enqueue(new IslandCell(r, c));

                    int[] rowDelta = { 1, -1, 0, 0 };
                    int[] colDelta = { 0, 0, 1, -1 };
                    while (queue.Count > 0)
                    {
                        IslandCell cell = (IslandCell)queue.Dequeue();
                        island.Add(cell);

                        for (int i = 0; i < rowDelta.Length; i++)
                        {
                            int row = cell.row + rowDelta[i];
                            int col = cell.col + colDelta[i];
                            if (row >= 0 &&
                                row < grid2.Length &&
                                col >= 0 &&
                                col < grid2[row].Length &&
                                grid2[row][col] == 1)
                            {
                                grid2[row][col] = 0;
                                queue.Enqueue(new IslandCell(row, col));
                            }
                        }
                    }

                    islands.Add(island);
                }
            }
        }

        int retVal = 0;
        foreach (List island in islands)
        {
            bool found = true;
            foreach (IslandCell cell in island)
            {
                if (grid1[cell.row][cell.col] == 0)
                {
                    found = false;
                    break;
                }
            }

            if (found)
            {
                retVal++;
            }
        }

        return retVal;
    }
}

Comments

Popular posts from this blog

Count Binary Substrings

Count Vowels Permutation: Standard DP

Maximum Number of Balloons