knaxuslearn · build · ship

week 8 of 15

Backtracking

Get comfortable with these coding mechanics first: building a path list, choose then explore then undo, copying the path when you save it, passing a start index. Template to type from memory by the end of the week: backtrack(state): if complete, save a copy. For each choice: add it, recurse, remove it.

  1. 66Subsets (opens LeetCode in a new tab)Include or skip with a start indexMedium
    Common mistake
    Saving path itself instead of a copy.
  2. 67Letter Combinations of a Phone Number (opens LeetCode in a new tab)Backtracking over positionsMedium
    Common mistake
    Empty input should return an empty list.
  3. 68Combination Sum (opens LeetCode in a new tab)Backtracking with reuseMediumcore
    Common mistake
    Using i+1 and blocking reuse.
  4. 69Permutations (opens LeetCode in a new tab)Used arrayMedium
    Common mistake
    Forgetting to unmark after returning.
  5. 70Generate Parentheses (opens LeetCode in a new tab)Backtracking with countsMedium
    Common mistake
    Checking the string instead of the counts.
  6. 71Combination Sum II (opens LeetCode in a new tab)Sort and skip duplicatesMedium
    Common mistake
    Skipping when i > 0 instead of i > start.
  7. 72Palindrome Partitioning (opens LeetCode in a new tab)Backtracking + palindrome checkMediumcore
    Common mistake
    Getting the slice bounds wrong (end is inclusive in this loop).
  8. 73Word Search (opens LeetCode in a new tab)Grid DFS with backtrackingMediumcore
    Common mistake
    Restoring the cell on every exit path.
  9. 74N-Queens (opens LeetCode in a new tab)Constraint backtracking (hard)Hardcore
    Common mistake
    Getting the diagonal formulas wrong. Use r - c and r + c.