← DFS / Recursion

Pattern Recognition Drill

#98 — Number of islands

Medium Advanced Graphs

The Problem

Count islands in a 2D grid of '1' (land) and '0' (water).

What approach would you use?

Think about it before scrolling down.

Key Signals

DFS / Recursion

For each unvisited '1', run DFS to mark all connected land. Count DFS starts. O(m*n).

Alt: Queue / BFS

BFS from each unvisited '1' also works. Same counting logic, same O(m*n).

Common Trap

This is connected components on a grid. Grid = implicit graph where neighbors are adjacent cells.

← #97 #99 →