Matrix traversal
A matrix is a grid of rows and columns. Most matrix problems are about visiting its cells in a planned order: row by row, layer by layer like a spiral, or from one corner backwards. Pascal's triangle is the same idea on a triangle-shaped grid, where each cell is built from the row above.
How to recognise it
- The input is a 2D grid and the answer depends on the order you visit cells (spiral, diagonal, layer by layer).
- The grid must be changed in place, such as a rotation, with O(1) extra memory.
- Each cell's value is built from cells already filled (the row above, the cell to the right or below).
- A pair of cells must satisfy an order rule, such as one strictly below and to the right of the other.
The idea in one picture
Think of a matrix as a city map of R rows and C columns. The cell at row r, column c is m[r][c]. Rows go down,
columns go right. Most matrix problems are not hard because of the data, but because of the route: which cell comes
next, and when to stop. Write the route down as clear boundaries, and the code follows.
Three routes you will meet
| Route | How it moves | Problem here |
|---|---|---|
| Layer by layer (spiral) | Four sides of the outer ring, then shrink the ring | Spiral Matrix |
| Swap across a line | Swap m[i][j] with m[j][i], then flip each row | Rotate Image |
| Backwards from a corner | Fill each cell from the cells below and to its right | Find a specific pair |
Template: the spiral with four boundaries
top = 0, bottom = R - 1, left = 0, right = C - 1
while top <= bottom and left <= right:
read row top from left to right; top = top + 1
read column right from top to bottom; right = right - 1
if top <= bottom: read row bottom from right to left; bottom = bottom - 1
if left <= right: read column left from bottom to top; left = left + 1
The two if checks matter when only one row or one column is left. Without them the last line is read twice.
Pascal's triangle: a grid built from the row above
Row r has r + 1 numbers. The edges are 1, and every inner number is the sum of the two numbers just above it.
| row | values |
|---|---|
| 0 | 1 |
| 1 | 1 1 |
| 2 | 1 2 1 |
| 3 | 1 3 3 1 |
| 4 | 1 4 6 4 1 |
The value at row r, column c is the binomial number C(r, c). So a row can also be built on its own, left to
right, using C(r, c + 1) = C(r, c) × (r - c) / (c + 1). Multiply first, then divide, and the division is always exact.
Common mistakes
- Swapping
m[r][c]around asm[c][r]in a matrix that is not square. Rows and columns have different lengths. - Transposing with both loops over the full range, which swaps every pair twice and changes nothing.
- Reading a corner cell twice in a spiral because the boundary moved after the loop instead of before the next side.
- Building Pascal values with
intproducts. The product can overflow before the division, so use a 64-bit value.
Problems
- Pascal's Triangleeasyguide ready
- Pascal's Triangle IIeasyguide ready
- Spiral Matrixmediumguide ready
- Pascal's Triangle IIImediumguide ready
- Rotate Imagemediumguide ready
- Find a specific pair in matrixmediumguide ready