Spiral Matrix
Spiral Matrix
Section titled “Spiral Matrix”
Medium
Day 10 • Striver Blind 75
📌 Problem Overview
Section titled “📌 Problem Overview”Given an m x n matrix, return all elements of the matrix in spiral order.
Examples & Constraints
Section titled “Examples & Constraints”Example 1:
- Input:
matrix = [[1,2,3],[4,5,6],[7,8,9]] - Output:
[1,2,3,6,9,8,7,4,5]
Constraints:
1 <= m, n <= 10
💡 Approach & Intuition
Section titled “💡 Approach & Intuition”4 boundaries (top, bottom, left, right) narrowed after each traversal leg.
🎯 Pattern Recognition
Section titled “🎯 Pattern Recognition”Matrix Boundary Shrinking
📊 Step-by-Step Execution (Mermaid Diagram)
Section titled “📊 Step-by-Step Execution (Mermaid Diagram)”graph TD Start["Input Data"] --> Process["Process Element by Element"] Process --> Lookup{"Hash Map / Set Lookup"} Lookup -- "Match Found" --> Return["Return Indices / Result"] Lookup -- "No Match" --> Store["Store in Map / Set"] Store --> Process🐢 Brute Force Solution
Section titled “🐢 Brute Force Solution”function spiralOrder(matrix) { const res = []; let top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1; while (top <= bottom && left <= right) { for (let c = left; c <= right; c++) res.push(matrix[top][c]); top++; for (let r = top; r <= bottom; r++) res.push(matrix[r][right]); right--; if (top <= bottom) { for (let c = right; c >= left; c--) res.push(matrix[bottom][c]); bottom--; } if (left <= right) { for (let r = bottom; r >= top; r--) res.push(matrix[r][left]); left++; } } return res;}- Time Complexity:
O(m*n) - Space Complexity:
O(1) - Explanation: Boundary contraction.
⚡ Optimized Solution
Section titled “⚡ Optimized Solution”function spiralOrder(matrix) { const res = []; let top = 0, bottom = matrix.length - 1, left = 0, right = matrix[0].length - 1; while (top <= bottom && left <= right) { for (let c = left; c <= right; c++) res.push(matrix[top][c]); top++; for (let r = top; r <= bottom; r++) res.push(matrix[r][right]); right--; if (top <= bottom) { for (let c = right; c >= left; c--) res.push(matrix[bottom][c]); bottom--; } if (left <= right) { for (let r = bottom; r >= top; r--) res.push(matrix[r][left]); left++; } } return res;}- Time Complexity:
O(m*n) - Space Complexity:
O(1) - Explanation: Layer-by-layer boundary shrinking traversal.
🐾 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”Traverse right, down, left, up while shrinking top/bottom/left/right boundary bounds.
💡 Progressive Hints
Section titled “💡 Progressive Hints”- Maintain top, bottom, left, right boundaries.