Number of Islands
grid is a map: 1 is land and 0 is water. An island is a group of land cells joined up, down, left or right (not diagonally). Return how many islands there are.
Constraints
- 1 ≤ rows, columns ≤ 80
- Every cell is 0 or 1.
Example 1
Input
grid = [[1,1,0,0,0],[1,1,0,0,0],[0,0,1,0,0],[0,0,0,1,1]]
Output
3
Why:
The 2 × 2 block, the single cell in the middle and the pair at the bottom right. Diagonal neighbours don't join.
Example 2
Input
grid = [[1,1,1],[0,1,0],[1,1,1]]
Output
1
Why:
All the land is joined through the middle column.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –