AceYourInterviews
Checking sign-in…

94. Maximum Area of Island

CoreClaim each land coordinate once, then measure its complete component

Find each group of ones connected through orthogonal neighbors and measure its cell count. Return the greatest such count.

Area means number of land cells, not perimeter or bounding-box area. Return zero if the grid contains no land.

Example 1

Input

[[1,1,0],[0,1,0],[1,0,1]]

Output

3

Explanation: The top component has three cells; two isolated bottom cells do not add to its area.

Example 2

Input

[[1,0],[0,1]]

Output

1

Explanation: Diagonal ones are two separate one-cell islands.

Constraints

  • 0 <= grid.length <= 105
  • 0 <= total grid cells <= 105
  • All rows have equal length and are non-null.
  • Cells are 0 or 1.
Hints
Hint 1

An island is a connected component of one-valued coordinates.

Hint 2

Use one shared visited matrix across the entire outer scan.

Hint 3

Count each popped cell and update best only after that component exhausts.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Component areas are compared after completion

[[1,1,0],[0,1,0],[1,0,1]]
3