PROBLEM 51 OF 75 · DEEP-DIVE PILOT

Number of Islands

Count groups of land in a rectangular grid. A land cell connects to land directly above, below, left, or right; diagonal contact alone does not connect two groups.

Know the exact contract

  • The local studio receives an array of equal-length strings containing only 0 for water and 1 for land. An empty array is allowed.
  • The input must not be changed. Count each connected land group exactly once using four-direction adjacency.
EXAMPLE 1["110", "010", "001"]→ 2

The upper-left land is one group. The lower-right land touches it only diagonally, so it is separate.

EXAMPLE 2["111", "010", "111"]→ 1

The center column joins the upper and lower rows into a single group.

EXAMPLE 3["000", "000"]→ 0

There is no land cell that can start an island.

From repeated searches to one marked traversal

SIMPLE APPROACH

For each land cell, independently search for all land reachable from it and compare those reachable sets. This repeats the same exploration many times and can approach quadratic work in the number of cells.

IMPROVED APPROACH

Scan the grid. When you find land not yet visited, count one new island and traverse from it using a stack or queue. Mark land as visited when adding it to the frontier so it cannot be scheduled twice. The traversal consumes that whole component before the scan continues.

Why it works

Before each new scan position, every visited land cell belongs to an island already counted. A new unvisited land cell therefore starts a different island. Four-direction traversal marks exactly the cells connected to that start, so no group is counted twice or skipped.

Cost

For m rows and n columns, the scan and traversal together inspect each cell a constant number of times: O(mn) time. The visited set and frontier can each hold O(mn) cells in the worst case.

A tempting mistake

In ["10", "01"], the two 1 cells touch diagonally but not orthogonally. An eight-direction flood fill would return one island; the required answer is two.

FOLLOW THE FRONTIER

Which land has already been claimed?

Each new unvisited land cell starts a group. Follow four-direction neighbors and notice that diagonal contact does not join groups.

STEP 1 / 4 · ISLAND 1

Inspect row 1, column 1. Frontier: (1, 2).

This unvisited land starts island 1. Mark it before exploring neighbors.
Result: 2
GUIDED CHECK · BEFORE YOU CODE

Can you defend the traversal?

These choices help you inspect the reasoning. A separate coding pass records an independent solve.

1. When does the island count increase?
2. Which neighbors belong to the same island?
3. When should a cell enter the visited set?
4. Why can the scan skip land already visited?
5. What is the worst-case cost for m rows and n columns?
INDEPENDENT SOLVE

Count each group only once.

Write solve(grid). grid is an array of equal-length strings made of 0 and 1. Return the number of four-direction land components without modifying grid. The local checks include empty, diagonal, and irregular grids.

Opening the code studio…

PROVE IT AGAIN TOMORROW

A fresh solve is the test.

Loading your saved attempts…

For additional practice, see the related LeetCode challenge.