Cascadiq

Number of Islands

algorithm · Level 2 · 25 min

Description

Given a 2D grid grid of '1's (land) and '0's (water), return the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically.

Requirements

  • Implement a function that takes grid (2D array of '1' and '0').
  • Return the number of connected components of '1's.

Constraints

- `1 <= m, n <= 300` - `grid[i][j]` is '1' or '0'.

Deliverables

Return the island count (integer).

Tags

ArrayDFSBFS