Skip to content
BASA DSA

Max Area of Island

MediumGraphs

Return the size of the largest orthogonally connected component of ones in a binary integer grid, or zero when there is no land.

Example 1

0,01I1
0,11I1
0,20
1,00
1,11I1
1,20
2,01I2
2,10
2,21I3
0 = water; 1 = land. Island labels distinguish 3 connected components.
Input
grid = [[1,1,0],[0,1,0],[1,0,1]]
Output
3

Explanation

The top-left component has three cells; both bottom corner cells are isolated.

Example 2

0,00
0,10
0 = water; 1 = land. Island labels distinguish 0 connected components.
Input
grid = [[0,0]]
Output
0

Explanation

No ones means maximum area zero.

Example 3

0,01I1
0 = water; 1 = land. Island labels distinguish 1 connected components.
Input
grid = [[1]]
Output
1

Explanation

The sole land cell has area one.

Constraints

  • 1 <= rows, columns <= 50
  • Cells are integer 0 or 1

Function contractmaxAreaOfIslandArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…