Max Area of Island

Given a 2D grid of 0s and 1s, return the maximum area of any island. The area of an island is the number of 1-cells in its connected component. Pattern focus: DFS. Explore each island fully and keep track of the largest component size found.

Input Format

grid = 2D binary matrix

Output Format

maximum island area

Constraints

  • 1 <= rows * cols <= 10^5
  • Each grid cell is either 0 or 1.

Examples

Example 1:

Input:

grid = [[0,0,1,0,0],[1,1,1,0,0],[0,1,0,0,1],[0,0,0,1,1]]

Output:

5

Explanation:

The largest connected island has 5 cells.

Example 2:

Input:

grid = [[0,0,0],[0,0,0]]

Output:

0

Explanation:

There is no island.

Example 3:

Input:

grid = [[1]]

Output:

1

Explanation:

A single land cell has area 1.

Loading...
Max Area of Island - Graph Traversal DSA Problem