Backtracking Flashcards

(9 cards)

1
Q

LeetCode 78 – Subsets
Return all subsets of a given set of numbers.

A

Use DFS/backtracking with index pointer.

At each step: choose to include nums[i] or skip it.

Append current path to result at every call.

Base: when index == n.

Time O(n·2ⁿ).

Tag: Backtracking, Power set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

LeetCode 39 – Combination Sum
All unique combinations where candidates sum to target (reuse allowed).

A

Sort candidates.

Backtrack with start index:

For each candidate ≥ start: add to path, recurse with target - val.

If target == 0 → save path.

Prune when val > target.

Time ~ O(N^(T/min)).

Tag: Backtracking, Pruning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

LeetCode 40 – Combination Sum II
Like Combination Sum, but each number used once.

A

Sort candidates.

At level, skip duplicates (if i>start and nums[i]==nums[i-1]).

Recurse with i+1.

Same pruning logic.

Tag: Backtracking, Deduplication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

LeetCode 46 – Permutations
All permutations of distinct numbers.

A

Track used elements in visited.

For each unused num: mark used, add, recurse, unmark.

Base: when path length == n.

Complexity O(n!).

Tag: Backtracking, Permute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

LeetCode 90 – Subsets II
Like Subsets but may contain duplicates.

A

Sort nums.

Backtrack with index.

At same depth, skip duplicates if nums[i]==nums[i-1].

Append snapshot of path at every call.

Tag: Backtracking, Deduplication.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

LeetCode 79 – Word Search
Check if word exists in grid (adjacent letters, no reuse).

A

For each cell = word[0], run DFS.

Keep visited (or modify board temporarily).

Explore 4 dirs with next char.

Backtrack (restore cell).

Complexity: O(MN·4^L).

Tag: Backtracking, DFS on grid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

LeetCode 131 – Palindrome Partitioning
Partition string into all palindrome substrings.

A

Backtrack with start index.

For end in [start..n): if s[start:end] is palindrome → recurse from end.

Append path when start==n.

Precompute palindrome table to optimize.

Tag: Backtracking, DP-check.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

LeetCode 51 – N Queens
Place n queens on an n×n board (no attacks).

A

Row-by-row backtracking.

Maintain cols, diag1 (r+c), diag2 (r−c).

At row r: for each col not in sets → place queen, recurse; backtrack.

Collect board on success.

Time O(n!).

Tag: Backtracking, Constraint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

LeetCode 17 – Letter Combinations of a Phone Number
Return all letter strings from digits (2–9).

A

Map digits → letters.

Backtrack with index: append each letter for current digit.

Base: when index==len(digits).

Complexity: O(3ⁿ–4ⁿ).

Tag: Backtracking, String.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly