Number of Islands
Number of Islands
Section titled “Number of Islands”
Medium
Day 6 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Given an m x n 2D binary grid of ‘1’s (land) and ‘0’s (water), return the number of islands. An island is surrounded by water and formed by connecting adjacent lands horizontally or vertically.
Examples & Constraints
Section titled “Examples & Constraints”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
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
Constraints:
1 ≤ m, n ≤ 300
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”The quintessential graph traversal problem testing connected component counting in a grid.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Pattern: Grid DFS
Iterate through every cell. When you find unvisited land, start a DFS to mark the entire island. Increment count.
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph TD Start["Start Node / Grid Cell"] --> Q["Initialize Queue / Stack / Visited Set"] Q --> Loop{"Is Queue / Stack Empty?"} Loop -- "No" --> Pop["Pop Current Node / Cell"] Pop --> Check{"Check Destination / Target"} Check -- "Found" --> Done["Return Path / Result"] Check -- "Not Found" --> Nbrs["Explore Neighbors (4-directions / Adjacency)"] Nbrs --> Push["Push Unvisited Neighbors"] Push --> Loop Loop -- "Yes" --> Done🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”// BFS/DFS with visited set- Time Complexity:
O(m×n) - Space Complexity:
O(m×n) - Explanation: DFS with a separate visited array.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function numIslands(grid) { if (!grid || !grid.length) return 0; const rows = grid.length, cols = grid[0].length; let count = 0; function dfs(r, c) { if (r < 0 || r >= rows || c < 0 || c >= cols || grid[r][c] === '0') return; grid[r][c] = '0'; dfs(r+1, c); dfs(r-1, c); dfs(r, c+1); dfs(r, c-1); } for (let r = 0; r < rows; r++) for (let c = 0; c < cols; c++) if (grid[r][c] === '1') { count++; dfs(r, c); } return count;}- Time Complexity:
O(m×n) - Space Complexity:
O(m×n) - Explanation: DFS with in-place marking (sinking islands).
🐾 Step-by-Step Walkthrough
Section titled “🐾 Step-by-Step Walkthrough”- Initialize State: Setup necessary pointers, dynamic programming arrays, or hash maps.
- Iterate & Evaluate: Process the input according to the boundary conditions.
- Update & Return: Compute the optimal answer and return early or at termination.
🎙️ FAANG Interview Pitch
Section titled “🎙️ FAANG Interview Pitch”- Frame as counting connected components
- DFS from each unvisited land cell
- Sink the island (mark as ‘0’) to save space
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Iterate every cell.
- When you find ‘1’, increment count and DFS to mark all connected land.
- Mark visited cells by changing ‘1’ to ‘0’.