All Patterns

Matrix

Traverse and transform 2D grids in place using index math for rows, columns, layers, and diagonals.

When to use

  • In-place rotation or transposition
  • Spiral, diagonal, or layer traversal
  • Marking cells without extra space
  • Coordinate index arithmetic

When NOT to use

  • The grid is really a graph (use BFS/DFS)
  • Cells have overlapping subproblems (use DP)

Common traps

  • Mixing up row and column indices
  • Mutating cells you still need to read
  • Boundary handling on the last layer

Key Invariant

Treat the grid as coordinates — most moves are arithmetic on (row, col), often handled layer by layer

Problems (5)

#33Set Matrix Zeroes
mediumFREE
#34Spiral Matrix
mediumFREE
#190Rotate Image
medium
#191Game of Life
medium
#193Diagonal Traverse
medium