Graph Theory37 sections · 1633 units
Open in Course

LeetCode 695 Max Area of Island - Problem Statement

DFS with size tracking

You're given a 22D grid of 00s (water) and 11s (land). An island is a group of 11s connected horizontally or vertically. Your task: find the island with the largest area and return that area. If there is no island, return 00.

For example, a 4×54 \times 5 grid might contain 33 islands of sizes 11, 44, and 66. You'd return 66. This is different from Number of Islands, where you counted components. Here, you need to measure each one and track the maximum.

You already know how to find connected components with DFS. The new challenge is making DFS return a value: the size of the component it explores.