Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Graphs

Max Area of Island

MEDIUM✓ CHIP-TIMEDLC #695 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Same connected-land idea as counting islands, but now measure size — return the cell count of the largest connected group of 1s in a 0/1 grid, or 0 if the grid is all water.

The same 0/1 grid setup as counting islands, but this time the question is size, not count: among every connected group of 1s, find the one with the most cells and report how many cells it contains.

Connectivity is still strictly orthogonal — a cell only joins its island through an up, down, left, or right neighbor, never a diagonal one — and an island can be as small as a single isolated 1.

When the whole grid is water, there's no island to measure, and the expected answer is simply zero rather than any error state.

  • grid up to a few hundred cells per side
  • values are exactly 0 or 1
  • connectivity is 4-directional only
  • an all-zero grid answers 0, not an error
EX 01
grid = [[0, 0], [0, 0]]
→ 0
all water
EX 02
grid = [[1]]
→ 1
single land cell
EX 03
grid = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
→ 9
full 3x3 island

The hintstake only what you need

HINT 1 THE NUDGE

Counting islands and measuring the biggest one are almost the same walk — the only new part is that each flood fill needs to report how many cells it covered.

HINT 2 THE STRUCTURE

Have the flood fill return its own size: 1 for the current cell plus whatever its four neighbours' fills return. Track the largest value seen across every start.

HINT 3 ONE STEP FROM THE ANSWER

DFS from every unvisited land cell, summing 1 + the four recursive calls; keep a running max and return 0 the moment you fall off the grid or hit water or an already-visited cell.

Coach's boardthe pattern, step by step

THE FLOOD TALLYPATTERN · DFS, SINK IN PLACEgrid = [[1,0,0],[1,1,0],[0,1,1]] — snake-shaped island
1
0
0
1
1
0
0
1
1
STEP 1

DFS floods from every unvisited 1, summing 1 plus each direction's own flood — and sinks each cell to 0 the moment it's counted.

STEP 1 / 7 · ← → WORK TOO

The splitstwo paces, two languages

grind/max-area-of-island.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def maxAreaOfIsland(self, grid: List[List[int]]) -> int:
        rows, cols = len(grid), len(grid[0])

        def area(r, c):
            if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] == 0:
                return 0
            grid[r][c] = 0
            return 1 + area(r + 1, c) + area(r - 1, c) + area(r, c + 1) + area(r, c - 1)

        best = 0
        for r in range(rows):
            for c in range(cols):
                if grid[r][c] == 1:
                    best = max(best, area(r, c))
        return best
TIME O(ROWS·COLS)SPACE O(1) EXTRA (RECURSION STACK ONLY)PYTHON · RACE PACE · 16 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED