Backtracking
Backtracking
·6 mins
Algorithm
Backtracking
Recursion
Dfs
Pruning
The choose, recurse, undo template for subsets, permutations, constrained placement, grid search, and pruning
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 301: Remove Invalid Parentheses
·4 mins
LeetCode
Hard
Backtracking
Bfs
String
LeetCode 301 note: compute the minimum left and right deletions before backtracking, or BFS one deletion per level and stop at the first valid level
LeetCode 996: Number of Squareful Arrays
·3 mins
LeetCode
Hard
Dynamic-Programming
Bitmask
Backtracking
Math
Hash-Table
LeetCode 996 note: with n at most 12, use bitmask DP: dp[mask][last] = permutations using the set mask and ending with last; dedupe by using equal values in index order
LeetCode 980: Unique Paths III
·2 mins
LeetCode
Hard
Backtracking
Matrix
Dfs
LeetCode 980 note: grid backtracking that marks visited cells in place and checks at the end that every empty cell was covered
LeetCode 51: N-Queens
·2 mins
LeetCode
Hard
Backtracking
LeetCode 51 note: one queen per row, with three sets tracking occupied columns and both diagonals
LeetCode 3799: Word Squares II
·2 mins
LeetCode
Medium
Backtracking
Enumeration
String
LeetCode 3799 note: four nested levels pruned by corner letters; sorting the words first makes the output lexicographic
LeetCode 22: Generate Parentheses
·2 mins
LeetCode
Daily
Medium
String
Backtracking
Recursion
Combinatorics
Parentheses
Generating all valid parentheses combinations using backtracking approach
LeetCode 2044: Count Number of Maximum Bitwise-OR Subsets
·2 mins
LeetCode
Medium
Backtracking
Bit-Manipulation
Enumeration
LeetCode 2044 note: the maximum OR is the OR of everything; once backtracking reaches it, count the rest as 2^k
LeetCode 988: Smallest String Starting From Leaf
·1 min
LeetCode
Medium
String
Backtracking
Tree
Depth-First-Search
LeetCode 988 note: DFS prepending each character so the string at a leaf already reads leaf to root, and keep the smallest
LeetCode 113: Path Sum II
·2 mins
LeetCode
Medium
Backtracking
Tree
Depth-First-Search
Binary-Tree
LeetCode 113 note: backtrack with the current path and store a copy at a leaf whose total matches; values can be negative, so no early pruning
LeetCode 473: Matchsticks to Square
·2 mins
LeetCode
Medium
Array
Dynamic-Programming
Backtracking
Bit-Manipulation
LeetCode 473 note: the total must be divisible by 4; place sticks from longest to shortest into one of four sides, skipping a side that would overflow or that equals a side already tried