Leetcode Problem 1905. Count Sub Islands
1905. Count Sub Islands
AI Mock Interview
Leetcode Solutions
DFS Traversal to Identify Sub-Islands
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Initialize a counter for sub-islands to 0.
Loop through each cell in
grid2
.
If a cell is part of an island (value is 1), perform DFS from that cell.
In DFS, check if the corresponding cell in
grid1
is not land (value is 0); if so, mark the flag as false.
Mark the cell in
grid2
as visited (set to 0) to avoid re-visiting.
Recursively call DFS for all 4-directionally adjacent cells that are part of the island in
grid2
.
After the DFS call, if the flag is still true, increment the sub-island counter.
Return the sub-island counter.
Iterative DFS with Explicit Stack for Sub-Island Detection
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...
Sign in with LinkedIn
Sign in with Github
OR
Sign in with Email link