Backtracking

Key points:

  • Use cases:

    • Output all results. This must be backtracking, at most with memorization.

    • Find how many ways to do xyz. DP, a typical DFS + memorization pattern.

  • One important idea of Backtracking is modify -> restore, like this one is different from traditional add, remove List: 212. Word Search IIarrow-up-right, tmp = board[r][c]; -> board[r][c] = '#'; -> board[r][c] = tmp;

Permutation

Combination / Subset

These are more of a DP:

https://leetcode.com/problems/partition-equal-subset-sumarrow-up-right https://leetcode.com/problems/partition-to-k-equal-sum-subsets/description/arrow-up-right

Template

https://discuss.leetcode.com/topic/46161/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partitioningarrow-up-right

Another type of backtracking

one common type is add -> remove. the other type is like drawing a tree, enumerate possible solutions https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/arrow-up-right https://leetcode.com/problems/generate-parentheses/description/arrow-up-right https://leetcode.com/problems/gray-code/description/arrow-up-right

Find all results

https://leetcode.com/problems/palindrome-partitioning/description/arrow-up-right

Last updated