↓Skip to main content

Matrix

Matrix
·5 mins
Algorithm Matrix Simulation Array
Transposing and rotating, converting between 2D indices and a 1D sequence, neighbors and diagonals, and spiral traversal with four boundaries
BFS and Graph Traversal
·6 mins
Algorithm Bfs Dfs Graph Matrix Union-Find
Connected components, grid flood fill, multi-source BFS, state-space BFS, shortest cycles, and recursion depth in DFS
LeetCode 827: Making A Large Island
·3 mins
LeetCode Hard Dfs Bfs Matrix Union-Find
LeetCode 827 note: label every island and record its size, then for each 0 add the sizes of the distinct neighboring islands plus 1
LeetCode 212: Word Search II
·2 mins
LeetCode Hard Trie Backtracking Matrix
LeetCode 212 note: build a Trie of all words, walk it during the grid DFS, remove words once found, and prune empty nodes
LeetCode 2267: Check if There Is a Valid Parentheses String Path
·3 mins
LeetCode Hard Dynamic-Programming Grid-Dp Matrix Bracket-Sequences
LeetCode 2267 note: the state is (r, c, open balance); the balance can be neither negative nor more than the remaining steps; search the states with BFS / DP and succeed at the end with balance 0
LeetCode 1210: Minimum Moves to Reach Target with Rotations
·3 mins
LeetCode Hard Bfs Matrix
LeetCode 1210 note: the BFS state is (tail position, orientation), each move checks its own empty cells, and equal step costs need no heap
LeetCode 4046: Minimum Cost Path With At Most K Turns
·3 mins
LeetCode Hard Graph Shortest-Path Heap Matrix
LeetCode 4046 note: Dijkstra with the last direction and the turns used in the state; for the same cell and direction, an earlier pop is cheaper, and if it also used fewer turns it dominates later states
LeetCode 773: Sliding Puzzle
·2 mins
LeetCode Hard Breadth-First-Search Dynamic-Programming Memoization Matrix
LeetCode 773 note: there are only 6! = 720 boards, so flatten each board into a string as a BFS state, with swaps of 0 and a neighbor as the moves
LeetCode 499: The Maze III
·3 mins
LeetCode Hard Graph Dijkstra Shortest-Path Heap Matrix
LeetCode 499 note: Dijkstra prioritized by (distance, path string), and the ball stops when it rolls over the hole
LeetCode 864: Shortest Path to Get All Keys
·3 mins
LeetCode Hard Bfs Bit-Manipulation Matrix
LeetCode 864 note: the BFS state is (position, bitmask of keys), so the same cell with different keys is a different node
LeetCode 2328: Number of Increasing Paths in a Grid
·2 mins
LeetCode Hard Dynamic-Programming Grid-Dp Memoization Topological-Sort Matrix
LeetCode 2328 note: strictly increasing means no cycles; dp[cell] = 1 + Σ dp[larger neighbors], processed from largest to smallest value (or by memoized DFS), summed over all cells
LeetCode 1292: Maximum Side Length of a Square with Sum Less than or Equal to Threshold
·3 mins
LeetCode Medium Binary-Search Prefix-Sum Matrix
LeetCode 1292 note: 2D prefix sums plus binary search on the side length; never mix the two binary search styles