WORLD 4
BACKTRACKING
Try a choice, explore everything after it, then undo it and try the next: every subset, every order, every sum.
STAGE 0 Backtracking tutorial
0:00 / 0:00
The move
Build an answer one choice at a time and undo the choices that lead nowhere. Three beats: choose (add a candidate), explore (recurse), un-choose (pop it so the next choice starts clean). Prune early when a partial answer can't work.
- Spot it
- The problem asks for all, every or generate, with combinations, permutations, subsets or placements, and the input is small.
- Cost
- Exponential: it visits every candidate, such as 2^n subsets or n! orders, so inputs stay small.
15 STAGES Backtracking problems
☆☆☆☆☆ Clear 5 to finish this world.
MEDIUM1:37Subsets
RecursionLC 78▶ START
MEDIUM1:40Subsets II
BacktrackingLC 90▶ START
MEDIUM1:42Combinations
BacktrackingLC 77▶ START
MEDIUM1:39Generate Parentheses
BacktrackingLC 22▶ START
MEDIUM1:40Letter Combinations
BacktrackingLC 17▶ START
MEDIUM1:36Permutations
BacktrackingLC 46▶ START
MEDIUM1:44Permutations II
BacktrackingLC 47▶ START
MEDIUM1:42Combination Sum
BacktrackingLC 39▶ START
MEDIUM1:52Combination Sum II
BacktrackingLC 40▶ START
MEDIUM1:45Palindrome Partitioning
BacktrackingLC 131▶ START
MEDIUM1:41Restore IP Addresses
BacktrackingLC 93▶ START
MEDIUM1:43Word Search
BacktrackingLC 79▶ START
HARD1:38Word Search II
Trie + backtrackingLC 212▶ START
HARD1:40N-Queens
BacktrackingLC 51▶ START
HARD1:41Sudoku Solver
BacktrackingLC 37▶ START