Skip to content

1905. Count Sub Islands #422

Answered by topugit
mah-shamim asked this question in Q&A
Aug 28, 2024 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We'll use the Depth-First Search (DFS) approach to explore the islands in grid2 and check if each island is entirely contained within a corresponding island in grid1. Here's how we can implement the solution:

Steps:

  1. Traverse the Grid: We'll iterate through each cell in grid2.
  2. Identify Islands in grid2: When we encounter a land cell (1) in grid2, we'll use DFS to explore the entire island.
  3. Check Sub-Island Condition: While performing DFS on an island in grid2, we'll check if all the corresponding cells in grid1 are also land cells. If they are, the island is a sub-island.
  4. Count Sub-Islands: For each island in grid2 that meets the sub-island condition, we'll increment our sub-island count.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mah-shamim
Comment options

mah-shamim Aug 28, 2024
Maintainer Author

@topugit
Comment options

topugit Aug 28, 2024
Collaborator

Answer selected by mah-shamim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants