Skip to content

Start typing to search patterns, problems and pages.

Graphs

Drop ink on any land square you have not coloured yet. It spreads to every square joined to it, but never crosses water. Each drop is one island, and counting the drops counts the islands.

⏱ about 25 minutesNeeds: 2D arrays, queuesTime O(m·n)Space O(m·n)

2 · Watch

Graphs in 60 seconds

A grid of land and water hides joined-up islands. Flooding again from every land cell repeats the same ground over and over.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

Number of Islands

or try
1–5 rows of 1s (land) and 0s (water), up to 6 wide, rows separated by spaces.
Scan the grid row by row. No islands yet.Step 1 of 12. 4 rows, 5 columns, 7 land cells. Scanning row by row. No islands yet.
Step 1 of 12
solution.ts
function numIslands(grid: string[][]): number {
let islands = 0;
for (let r = 0; r < grid.length; r++) {
for (let c = 0; c < grid[0].length; c++) {
if (grid[r][c] !== "1") continue;
islands++;
grid[r][c] = "0";
const queue: [number, number][] = [[r, c]];
for (let head = 0; head < queue.length; head++) {
const [cr, cc] = queue[head];
for (const [dr, dc] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) {
const nr = cr + dr, nc = cc + dc;
if (grid[nr]?.[nc] === "1") {
grid[nr][nc] = "0";
queue.push([nr, nc]);
}
}
}
}
}
return islands;
}
▶ marks the line running now

4 · Your turn

0 of 8 problems solved

Start from this

// Mark on push, so no node is queued twice.
const seen = new Set([start]);
const queue = [start];
// Read with an index: shift() is O(n) in JavaScript.
for (let head = 0; head < queue.length; head++) {
  // Oldest first: the closest nodes go first.
  const node = queue[head];
  for (const next of neighbours(node)) {
    if (seen.has(next)) continue;
    seen.add(next);
    queue.push(next);
  }
}

Easy ways to go wrong

  • Marking a node when you pop it instead of when you push it, so it’s queued twice.
  • Forgetting the graph may have more than one connected piece.
  • Using DFS for a shortest path. BFS finds it when every edge costs the same; different costs need Dijkstra.
  • Counting steps per node popped instead of per layer. Finish the whole layer, then add one.

Flood fill

Start at one cell or node and spread to every neighbour you haven’t seen yet.

  1. Medium01

    Number of Islands

    LeetCode 200 · Blind 75 · NeetCode 150

    The one you just watched. Write it from memory.

  2. Medium02

    Clone Graph

    LeetCode 133 · Blind 75 · NeetCode 150

    Same flood, but copy each node as you claim it.

  3. Medium03

    Pacific Atlantic Water Flow

    LeetCode 417 · Blind 75 · NeetCode 150

    Flood from both oceans, keep the overlap.

Topological order

Take whatever has nothing left to wait for. If some never free up, there’s a loop.

  1. Medium04

    Course Schedule

    LeetCode 207 · Blind 75 · NeetCode 150

    Take what has nothing left to wait for. If some never free up, there’s a loop.

  2. Medium05

    Course Schedule II

    LeetCode 210 · NeetCode 150

    Same queue as Course Schedule. The order you take courses in is the answer.

    Watch it run

Fewest steps with BFS

BFS moves out one layer at a time, so the first time it reaches something is the shortest way.

  1. Medium06

    Rotting Oranges

    LeetCode 994 · NeetCode 150

    Put every rotten orange in the queue first. Each BFS layer is one minute.

    Watch it run
  2. Hard07

    Word Ladder

    LeetCode 127 · NeetCode 150

    Words are nodes, and one-letter changes are the links. BFS finds the fewest.

  3. Hard08

    Bus Routes

    LeetCode 815

    Search over buses, not stops. Each bus you board is one more layer.

UP NEXT · LESSON 14 OF 20Advanced GraphsCheapest paths with Dijkstra, joined groups with Union-Find.