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

Leetcode Problem 305. Number of Islands II

305. Number of Islands II

Leetcode Solutions

Key approach of the solution: Union-Find

  1. Define the Union-Find class with methods find, union, addLand, isLand, and numberOfIslands.
  2. Initialize the Union-Find instance with a size of m * n.
  3. Iterate over each position in positions and convert the 2D cell to a 1D index.
  4. Add the land to the Union-Find structure and increment the island count.
  5. Check all four neighbors of the new land cell. If a neighbor is land, perform a union operation.
  6. After each land addition, append the current number of islands to the answer list.
  7. Return the answer list containing the number of islands after each addition.
UML Thumbnail

Key approach of the solution: Depth-First Search (DFS)

Ask Question

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

Suggested Answer

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