Pond Sizes

You have a matrix of non-negative integers representing land heights. A pond is defined by contiguous cells of height 0 (connected 4-directionally). Return a list of the sizes of all ponds (areas of connected zeros). This is similar to counting connected components via DFS.

Input Format

2D list of ints (>=0).

Output Format

List of pond sizes (ints).

Constraints

  • dimensions ≤ 100.

Examples

Example 1:

Input:

land = [[0,2,1],[0,0,1],[1,1,0]]

Output:

[4]

Explanation:

There is one pond of size 3 (three zeros connected) and one of size 1.

Loading...
Pond Sizes - Matrix DSA Problem