Blind 75 · Graphs

Number of Islands

Medium

Watch the walkthrough (6:04)

Problem

Given a grid of land and water, grid, where '1' is land and '0' is water, return the number of islands.
An island is land cells joined up, down, left or right, with water all around it.
Cells that only touch at a corner are not joined, and everything past the grid's edge is water.

Examples

Example 1:
Input: grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]
Output: 1

Example 2:
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3

Example 3:
Input: grid = [["1"]]
Output: 1

Example 4:
Input: grid = [["1","0","1","0","1"]]
Output: 3

Constraints

1 <= rows, columns <= 300
Every row has the same length; cells are '1' or '0'.

Prerequisites

Graph traversal — DFS and BFS, and a visited set as the thing that makes both terminate.
Union-find for connectivity questions; topological order for dependency ones.

How to think about it

1. Sink Each Island as You Find It Optimal

Intuition

Every time you step on new land, sink its whole island and add it to the count. Scan the grid row by row: a cell still holding '1' is land no island counted so far has reached, so it starts a new one. Sinking sets the cell to '0' and tries its four neighbours, down, up, right and left, sinking every '1' it reaches, so the whole island is under water before the scan moves on, and a cell of it the scan meets later is water and is not counted again. Corners are never tried, so islands that only touch at a corner stay apart. The grid itself is the record of what is sunk: no other grid or set is kept.

Algorithm

1. Keep the grid, for sink to reach, and start the count at 0.
2. Scan every cell, row by row. When a cell is land, sink its whole island and add 1 to the count.
3. In sink(r, c): off the grid, return. Not land (water, or land already sunk), return.
4. Otherwise set the cell to '0', and sink down, up, right and left.
5. When the scan is done, the count is the answer.

Time & Space

Time O(r·c) for an r by c grid: the scan visits each cell once, and each cell is sunk at most once, with four tries. Space O(r·c) at worst: the calls go as deep as the biggest island, and a huge island can overflow the call stack (in Python, from about 1,000 cells of one island, the default recursion limit). The second approach's Python keeps a stack of its own, which a huge island cannot overflow.

2. Flood Fill, the Grid Left Untouched Optimal

Intuition

Every unvisited land cell begins exactly one island — so scan the grid, and each time you meet one, flood its whole island as visited before moving on. The flood is what stops the same island being counted from its other cells, and whether you use DFS, BFS or union-find changes nothing about that argument.

Algorithm

1. Scan every cell.
2. Land that is not yet visited: count one island.
3. Flood-fill from it, marking all connected land visited.
4. Continue the scan.

Time & Space

Time O(rows x columns) — each cell visited a constant number of times. Space O(rows x columns) worst case for the visited set and recursion.