Solving Number of Islands using DFS/BFS.

Problem

Count connected components in grid.

Pattern

This problem demonstrates the DFS/BFS pattern.

Approach

DFS flood fill on ‘1’ cells.

Solution

  // Solution for Number of Islands
// Pattern: DFS/BFS
// O(m×n) time
  

Complexity

O(m×n) time

Best Practices

  • Identify the pattern before coding — pattern recognition saves time
  • Handle edge cases: empty input, single element, duplicates
  • Use descriptive variable names even in timed interviews
  • Test with the provided examples plus one custom case