#39
Medium
Backtracking

Combination Sum

Find all combinations that sum to a target, with unlimited reuse of candidates.

Backtracking

Pattern fit

This is the backtracking pattern where the recursion keeps an index to avoid duplicate ordering while still allowing the current value to be reused.

Key observation

The same index can be chosen again because repetition is allowed, but earlier indices should not be revisited after moving forward.

Target complexity

Exponential / O(target)

How to break down the solution cleanly

1

This is the backtracking pattern where the recursion keeps an index to avoid duplicate ordering while still allowing the current value to be reused.

2

The same index can be chosen again because repetition is allowed, but earlier indices should not be revisited after moving forward.

3

Model the decision tree explicitly.

4

Choose the next candidate set for the current depth.

Reference implementation

Python
# Generic pattern template
def backtrack(path):
    if complete(path):
        answer.append(path[:])
        return
    for choice in choices(path):
        if not valid(choice, path):
            continue
        path.append(choice)
        backtrack(path)
        path.pop()

Common pitfalls

warning

Incrementing the start index even when reuse is allowed.

warning

Failing to stop when the remaining target becomes negative.

Common follow-ups

How does Combination Sum II differ?

Why is sorting useful for pruning here?

Continue with related problems

Build repeatable depth inside the Backtracking cluster before moving on.

view_weekBack to the pattern page
LeetCode 39. Combination Sum Guide | Interview AiBox