Pattern Recognition Drill
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.
For each unvisited '1', run DFS to mark all connected land. Count DFS starts. O(m*n).
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.