TRT
Sign in
mediumAdvancedLeetCode#flood fill with a count

Max area of island

Find the largest island in a grid by having each flood fill return the number of cells it marked.

The Runtime Theory Team1 min read
Taught inDepth-first searchPro — members onlyDepth-first search follows one path as far as it can, then backs up and tries the next. It is the natural way to flood a region, explore every possibility, or ask what is reachable, as long as you watch its depth.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

ingrid = [[0, 0, 1, 0], [1, 1, 1, 0], [0, 1, 0, 0]]

out5

ingrid = [[0, 0, 0], [0, 0, 0]]

out0

ingrid = [[1]]

out1

Given a grid of 1s (land) and 0s (water), return the area of the largest island, or 0 if there is no land.

Before you code

It is the island-counting scan with one change: the flood fill returns how many cells it marked.

Hints

  1. area(r, c) returns 0 for water, out-of-range or visited cells.
  2. Otherwise mark the cell and return 1 + area of the four neighbors.
  3. Keep the maximum over all starts.

Production & complexity notes

When solving Max area of island on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Depth-first search to reinforce the underlying pattern.

What it practices

DFS that returns a value, from Depth-first search.

Not started

Sign in to save your learning progress.

Sign in to save