2Brute2Best
🔥0

Number of Islands

Medium·LeetCode #200 Pro OnlyBasic Reinforcement
LeetCode
01

Problem Statement

You are given a map as a grid of characters. '1' is land and '0' is water. An island is a group of land cells joined up, down, left or right. Return how many islands there are. Two land cells that touch only at a corner are not joined.

Input

grid, an m × n grid whose cells are the characters '0' or '1'.

Output

The number of islands.

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

All the land cells are joined into one island.

Example 2
Input: grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]
Output: 3

The top-left 2×2 block is one island. The cell (2,2) touches that block only at a corner, so it is its own island. The cells (3,3) and (3,4) form the third island.

Constraints
  • • 1 ≤ m, n ≤ 300, 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:

“a group of land joined up, down, left or right”
→ One island is one connected group. One search from one land cell visits all of it.
“corner touching does not join”
→ Four moves only. A diagonal land cell is a different island.
“how many islands”
→ Count the searches, not the land cells: one count for each search that starts from new land.
03

Approach Complexity Ladder

Brute Force — label propagation until the names stop changingO((m · n)²)O(m · n)

Give each land cell its own name, let neighbours pass the smallest name, then count the names

Optimal — scan once, and sink each new island with a DFSO(m · n)O(m · n)

Scan the grid; each land cell still unreached starts a new island: count it and sink the whole island

Brute Force Intuition

Give every land cell a unique name, its own index. Sweep the grid, and each land cell takes the smallest name among its land neighbours. Repeat sweeps until no name changes. Each island then shares one name, so the answer is the number of different names.

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.