LeetCode 695 Max Area of Island - Example and Complexity Analysis

Walkthrough and analysis

Scan grid for 1's.

Find first 1 at some position. DFS: count connected 1's. Say area = 4.

Continue scanning. Find another island. DFS: area = 6. Update max = 6.

Continue. Find more islands with smaller areas.

Return max = 6.

Each cell visited once. O(mn)O(m \cdot n) time. Recursion stack: O(mn)O(m \cdot n) worst case.