Max Area of Island

Med
Code
Loading editor...
Tap Analyze to see visualization
Variables

Run code to see variables

Output

Console output will appear here

Press Space to start to step? all shortcuts

Solution Guide: Max Area of Island

Approach

For each land cell, DFS returns the area of its full island by summing all reachable land neighbors. Keep global max across all starts.

Complexity Analysis

Time
O(m * n)
Space
O(m * n)

Pattern

Grid DFS + Component Size

Why It Works

Every cell is visited at most once and contributes to exactly one component count. Returning 1 + recursive contributions gives the component area.

Updated Feb 2026