ALG-M016MediumGraph Traversal
Count Land Regions
A rectangular grid uses 1 for land and 0 for water. Count connected land regions using only up, down, left, and right adjacency.
Contract
input grid: integer[][]
return integer
Constraints
- • Grid entries are 0 or 1
- • All rows equal length
Examples
Example 1
grid = [[1, 1, 0], [0, 1, 0], [0, 0, 1]]
→ 2
Example 2
grid = [[0, 0], [0, 0]]
→ 0
5 judge tests (2 shown above). Target complexity: O(rows*cols).