← All problems

Number of Islands (Easy Intro)

Easy graphsdfsbfsmatrix LeetCode ↗
Given an m x n binary grid where '1' represents land and '0' water, count the number of islands. An island is a group of '1's connected 4-directionally, surrounded by water. Assume the grid border is surrounded by water. Solve it by sinking each discovered island with DFS.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(m*n), space O(m*n) worst-case recursion depth.

Common pitfalls

  • Counting the same island twice by not marking visited cells.
  • Using 8-directional adjacency instead of 4-directional.
  • Stack overflow on huge grids — consider iterative BFS for production.

Your solution

Log in to save your code and track progress.