← 返回 amazon 的题目列表Calculate Sizes of Islands in 2D Binary Grid
类型:online_judge
amazon
Design an algorithm to calculate the size of each island in a given 2D binary grid. Islands are surrounded by water (0) and consist of land (1) connected horizontally or vertically. Write a function that takes an m x n 2D array grid as input, where 1 represents land and 0 represents water, and returns a list of the sizes of each island.
Input
An m x n 2D array grid, with values 0 or 1.
Output
A list of island sizes, in any order.
Example
Input:
grid = [
[1,1,0,0,0],
[1,1,0,0,0],
[0,0,0,1,1],
[0,0,0,1,1]
]
Output:
[4, 4]
Constraints
m, n are within the range of [1, 100].
Example
Input
1
5 5
1 1 0 0 0
1 1 0 0 0
0 0 0 1 1
0 0 0 1 1