Matrix Algorithms
matrix-algorithms
Data Structures
Matrix/Grid Fundamentals
Picture an image as a grid of pixels, a chessboard as an 8 by 8 array of squares, or a maze as a rectangle of walls and corridors. Each of these is a **Matrix/Grid**: a two-dimensional array indexed by `(row, column)` where the right traversal order can turn a hard problem into a single nested loop. This lesson covers how to allocate and initialize 2D arrays in JavaScript and Python, how row-major storage maps a `[i][j]` access to a contiguous memory offset, and the standard traversal patterns: row by row, column by column, and along the two diagonals. You will also practice boundary checks, the most common source of off-by-one bugs in grid problems, and implement a transpose to see how index swaps reshape data in place. In **Arrays & Strings**, you saw that a one-dimensional array gives `O(1)` indexed access through a single offset calculation. A grid is just nested arrays sharing that same property along two axes, so every shifting and bounds idea you used there carries over directly. Once you can move confidently through a grid, **Linked Lists (Singly)** shifts the focus to a very different memory layout: pointer-based nodes with no contiguous block at all.
Not Started
0%
Algorithms
Matrix Algorithms
Rotating an `n x n` image 90 degrees clockwise sounds like it needs a second matrix to hold the output. The standard trick is two passes over the same matrix: transpose it (swap rows and columns), then reverse each row, and the rotation appears in place with no extra memory. Almost every matrix interview problem hides a similar two-step decomposition under what looks like a hard spatial puzzle. **Matrix Algorithms** trains those decompositions. You will implement spiral, diagonal, snake, and anti-diagonal traversals by managing the four boundary indices that change as the walk continues. You will rotate by 90, 180, and 270 degrees in place. You will search sorted matrices three ways: per-row binary search, the staircase search that runs in `O(m + n)` on row-and-column-sorted matrices, and a single binary search when the whole matrix is sorted. The lesson finishes with in-place transformations like "set matrix zeroes" and Conway's Game of Life that encode state inside the matrix itself. In **Matrix/Grid Fundamentals**, you learned how 2D arrays are laid out and indexed. **Iteration Patterns on Arrays/Strings** taught you single and nested loops in 1D; matrix algorithms layer those patterns across rows and columns with careful boundary management. From here, **Dynamic Programming (Advanced)** revisits 2D structures, this time as DP tables.
Not Started
0%
Practice Problems
Construct Quad Tree
Given an n x n binary matrix, construct a Quad-Tree representation by recursively partitioning the grid into four equal quadrants.
Game of Life
Implement Conway's Game of Life: compute the next state of a board in-place using state-encoding to avoid extra space.
Minimum Path Sum
Given an m x n grid filled with non-negative numbers, find a path from the top-left to the bottom-right that minimizes the sum of all numbers along the path.
Rotate Image
Rotate an n x n 2D matrix by 90 degrees clockwise in-place, without allocating another matrix.
Set Matrix Zeroes
Given an m x n integer matrix, if an element is 0, set its entire row and column to 0 using constant extra space.
Spiral Matrix
Return all elements of an m x n matrix in spiral order, traversing from the outer boundary inward.
Unique Paths II
Given an m x n grid with obstacles, count the number of unique paths from the top-left corner to the bottom-right corner, moving only right or down.
Valid Sudoku
Determine if a 9x9 Sudoku board is valid by checking rows, columns, and 3x3 sub-boxes for duplicate digits using hash sets.
01 Matrix
Given a binary matrix, find the distance of each cell to the nearest 0 using multi-source BFS.
Community
Shortest Path in Binary Matrix
BFS through 0-cells in 8 directions to find the shortest path from top-left to bottom-right.
Dungeon Game
Find the minimum starting health a knight needs to traverse a dungeon grid from top-left to bottom-right, using REVERSE bottom-up DP from the princess cell.
Cherry Pickup
Maximize cherries collected by walking from top-left to bottom-right and back, modeled as two simultaneous forward walks with shared cells counted once.
Minimum Falling Path Sum
Find the minimum sum of a top-to-bottom path in a square matrix where each step moves to one of three cells directly below, using row-by-row DP.
Sudoku Solver
Solve a partially-filled 9x9 Sudoku in place using backtracking with row, column, and 3x3-box constraint sets.
