1905. Count Sub Islands

Count Sub Islands (LeetCode 1905) is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph DFS - Connected Components / Island Counting subpattern.

How to solve Count Sub Islands

Count Sub Islands is a DFS Connected Components problem, part of Graph Traversal Patterns (DFS & BFS). Use DFS to find connected components and count islands in grids. Flood fill and province counting problems.

Work towards the solution in these steps:

  1. Let's use floodfill to iterate over the islands of the second grid
  2. Let's note that if all the cells in an island in the second grid if they are represented by land in the first grid then they are connected hence making that island a sub-island

The reference solution runs in O(m * n) time and O(m * n) space.

The Count Sub Islands solution and editorial walks through the full approach with reference code.

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.

Topics and companies

Topics: Array, Depth-First Search, Breadth-First Search, Union Find, Matrix.

How to practise Count Sub Islands on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(m * n) time and O(m * n) space.

Open Count Sub Islands in the code editor, or read the Count Sub Islands editorial for a worked solution with its approach and complexity analysis.

Where Count Sub Islands sits in the DSA pattern sheet

Problems related to Count Sub Islands

Other problems that use the same Graph DFS - Connected Components / Island Counting and Graph Traversal Patterns (DFS & BFS) techniques:

Count Sub Islands FAQ

What is the LeetCode number of Count Sub Islands?

Count Sub Islands is LeetCode problem 1905, rated Medium.

What pattern is used to solve Count Sub Islands?

Count Sub Islands is a DFS Connected Components problem. It belongs to the Graph Traversal Patterns (DFS & BFS) pattern, in the Graph DFS - Connected Components / Island Counting subpattern.

What is the time complexity of Count Sub Islands?

The reference solution runs in O(m * n) time and O(m * n) space.

Preparing this workspace.