Skip to main content

๐Ÿ”ข Matrix

One-line summary: 2D arrays โ€” master spiral traversal, layer-by-layer rotation, and diagonal/anti-diagonal iteration.


Conceptโ€‹

Matrix Traversal Matrix GIF

Operations: row-by-row, column-by-column, spiral, diagonal, layer rotation, BFS/DFS on grid.

Key trick: matrix[row][col] โ†’ after 90ยฐ rotation: rotated[col][n-1-row].


Time & Space Complexityโ€‹

OperationTimeSpace
Traverse all cellsO(mยทn)O(1)
Spiral traversalO(mยทn)O(1)
Rotate in-placeO(nยฒ)O(1)
BFS/DFS on gridO(mยทn)O(mยทn)

Common Patternsโ€‹

Spiral Orderโ€‹

function spiralOrder(matrix) {
const result = [];
let top = 0,
bottom = matrix.length - 1,
left = 0,
right = matrix[0].length - 1;
while (top <= bottom && left <= right) {
for (let i = left; i <= right; i++) result.push(matrix[top][i]);
top++;
for (let i = top; i <= bottom; i++) result.push(matrix[i][right]);
right--;
if (top <= bottom) {
for (let i = right; i >= left; i--) result.push(matrix[bottom][i]);
bottom--;
}
if (left <= right) {
for (let i = bottom; i >= top; i--) result.push(matrix[i][left]);
left++;
}
}
return result;
}

Pascal's Triangle Rowโ€‹

function getRow(rowIndex) {
const row = new Array(rowIndex + 1).fill(0);
row[0] = 1;
for (let i = 1; i <= rowIndex; i++) for (let j = i; j > 0; j--) row[j] += row[j - 1];
return row;
}

Pitfallsโ€‹

  • Spiral: guard top <= bottom and left <= right before reverse passes
  • BFS on grid: mark visited immediately when enqueuing, not when dequeuing
  • In-place rotation: transpose first, then reverse each row

Practice Problemsโ€‹

ProblemDifficultySolution
LC 54 โ€” Spiral MatrixMediumView Solution
LC 59 โ€” Spiral Matrix IIMediumView Solution
LC 118 โ€” Pascal's TriangleEasyView Solution
LC 119 โ€” Pascal's Triangle IIEasyView Solution
LC 48 โ€” Rotate ImageMedium
LC 73 โ€” Set Matrix ZeroesMedium
LC 733 โ€” Flood FillEasy
LC 74 โ€” Search a 2D MatrixMedium
LC 240 โ€” Search a 2D Matrix IIMedium
CC โ€” Matrix Rotation (MTRNSFRM)Medium
LC 542 โ€” 01 MatrixMedium
LC 36 โ€” Valid SudokuMedium
LC 37 โ€” Sudoku SolverHard
LC 79 โ€” Word SearchMedium
LC 130 โ€” Surrounded RegionsMedium
LC 200 โ€” Number of IslandsMedium
LC 212 โ€” Word Search IIHard
LC 221 โ€” Maximal SquareMedium
LC 289 โ€” Game of LifeMedium
LC 378 โ€” Kth Smallest Element in a Sorted MatrixMedium
LC 463 โ€” Island PerimeterEasy
LC 498 โ€” Diagonal TraverseMedium
LC 695 โ€” Max Area of IslandMedium
LC 766 โ€” Toeplitz MatrixEasy
LC 867 โ€” Transpose MatrixEasy
LC 885 โ€” Spiral Matrix IIIMedium
LC 994 โ€” Rotting OrangesMedium
LC 1091 โ€” Shortest Path in Binary MatrixMedium
LC 1329 โ€” Sort the Matrix DiagonallyMedium
LC 1380 โ€” Lucky Numbers in a MatrixEasy
LC 1572 โ€” Matrix Diagonal SumEasy
LC 1905 โ€” Count Sub IslandsMedium
CC โ€” Matrix Multiplication (MATMUL)Medium
CC โ€” Grid Paths (GRIDPATH)Medium
CC โ€” 2D Array Sum (ARRAY2D)Easy

  • Two Pointers โ€” converging pointers on matrix rows/columns
  • Graphs โ€” BFS/DFS on grid = graph traversal
  • Binary Search โ€” 2D sorted matrix search

โ† Back to Home ยท ยฉ sparshjaswal