200. Number of Islands
Problem
Given an m x n 2D binary grid grid which represents a map of '1' s (land) and '0' s (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
Example 2:
Constraints:
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 300
- grid[i][j] is '0' or '1'.
Approach
To solve this, we can use a depth-first search approach.
The idea is that we iterate through the grid and when we see a 1 we mark it as a 0, then recursively check all of it's neighbours and do the same until the entire island has been marked as 0. Then, we can increment our islands counter.
This ensures that we only process every element once because they will be marked as zeros if it's part of a previously processed island.
Complexity
Time: O(m * n)
We iterate through the entire grid once.
Space: O(1)
No extra space is created.