2Brute2Best
🔥0

Max Area of Island

Medium·LeetCode #695 Pro OnlySlight Variation
LeetCode
01

Problem Statement

You are given a grid of 0s (water) and 1s (land). An island is a group of land cells joined up, down, left or right. The area of an island is how many cells it has. Return the largest area of any island. If the grid has no land, return 0.

Input

grid, an m × n grid of 0s and 1s.

Output

The largest island area, or 0 if there is no land.

Example 1
Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6

The largest island is the group around the middle-right, with 6 cells. Cells that touch only at a corner do not join, so they are not counted.

Example 2
Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

There is no land, so the largest area is 0.

Constraints
  • • 1 ≤ m, n ≤ 50, where m is the number of rows and n is the number of columns
  • • grid[i][j] is 0 or 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“group of land joined up, down, left or right”
→ One island is one connected group, and one search measures it.
“area of an island”
→ A search can return a number: the count of cells it reaches. Adding the returns up gives the size.
“largest area”
→ Keep a best value across the searches. It is a maximum, not a sum.
03

Approach Complexity Ladder

Brute Force — a fresh BFS from every land cellO((m · n)²)O(m · n)

From each land cell, run a new search to measure its island; keep the largest size

Optimal — one DFS per island, returning its sizeO(m · n)O(m · n)

Scan the grid; each unmarked land cell starts a DFS that returns the size of its island

Brute Force Intuition

For every land cell, run a BFS with its own new visited array. It counts the cells it reaches, which is the size of that cell's island. Keep the largest count. Every island is measured once for each of its cells, so the same work is repeated many times.

Recognition rule for this sub-pattern:

Start a DFS/BFS from each unvisited cell; everything it reaches is one component.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.