bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1905. Count Sub Islands

1905. Count Sub Islands

Leetcode Solutions

DFS Traversal to Identify Sub-Islands

  1. Initialize a counter for sub-islands to 0.
  2. Loop through each cell in grid2.
  3. If a cell is part of an island (value is 1), perform DFS from that cell.
  4. In DFS, check if the corresponding cell in grid1 is not land (value is 0); if so, mark the flag as false.
  5. Mark the cell in grid2 as visited (set to 0) to avoid re-visiting.
  6. Recursively call DFS for all 4-directionally adjacent cells that are part of the island in grid2.
  7. After the DFS call, if the flag is still true, increment the sub-island counter.
  8. Return the sub-island counter.
UML Thumbnail

Iterative DFS with Explicit Stack for Sub-Island Detection

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...
bugfree Icon
OR