
Combination Sum
Treat this as an enumeration problem, not a permutation problem. Sort the candidates, keep combinations in nondecreasing order, recurse from the same index…
View solutionExplore constrained choices recursively, prune invalid paths, and undo state cleanly.
Problems
Practice problems that share this primary solution pattern and compare the clues that reveal it.

Treat this as an enumeration problem, not a permutation problem. Sort the candidates, keep combinations in nondecreasing order, recurse from the same index…
View solution
The hard part is not finding combinations that add to the target. It is finding them once while respecting the physical number of occurrences in the input.
View solution
Generate only prefixes that can still become valid. The balance state tells you exactly which branches to keep.
View solution
Backtracking becomes much easier when you can name what one recursive call means. Here, each call assigns one phone-keypad digit, and each complete path…
View solution
The phrase “try digits and backtrack” is the easy part. The interview-grade solution keeps four representations synchronized: the board, row constraints,…
View solution
The duplicate-ordering trap is the whole problem: [1, 2] and [2, 1] represent one selection, not two. Build every path in increasing order, and the…
View solution
The board is only the output surface. The real N-Queens solution is a depth-n search over column assignments, with three constraints checked before each…
View solution
The key change from N-Queens is the output contract: you need one integer, so the search should retain only reversible constraints and count valid leaves.
View solution
The search tree is easy to recognize and easy to corrupt. Build one position at a time, choose an unused value, recurse, then undo exactly that choice.
View solution
When nums = [1, 1, 2], ordinary permutation backtracking treats the two 1 values as different input positions. That creates duplicate value sequences.
View solution
The trap is to think “place three dots.” The useful model is narrower: choose exactly four contiguous digit segments, validate each one immediately, and…
View solution
The duplicate bug comes from treating equal input positions as different decisions. Sort first, then skip equal candidates only when they are siblings at…
View solution
A grid DFS can match the right letters and still be wrong. The missing piece is path-local state: mark a cell when you enter it, explore from that choice,…
View solution