0
Leetcode Problem 305. Number of Islands II
305. Number of Islands II
AI Mock Interview
Leetcode Solutions
Key approach of the solution: Union-Find
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Define the Union-Find class with methods
find
,
union
,
addLand
,
isLand
, and
numberOfIslands
.
Initialize the Union-Find instance with a size of
m * n
.
Iterate over each position in
positions
and convert the 2D cell to a 1D index.
Add the land to the Union-Find structure and increment the island count.
Check all four neighbors of the new land cell. If a neighbor is land, perform a union operation.
After each land addition, append the current number of islands to the answer list.
Return the answer list containing the number of islands after each addition.
Key approach of the solution: Depth-First Search (DFS)
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