Combination Sum
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. The same number may be chosen from candidates an unlimited number of times.
Examples
candidates = [2,3,6,7], target = 7
[[2,2,3],[7]]
2+2+3=7 and 7=7.
candidates = [2,3,5], target = 8
[[2,2,2,2],[2,3,3],[3,5]]
Multiple combinations sum to 8.
Constraints
- •
1 <= candidates.length <= 30 - •
2 <= candidates[i] <= 40 - •
All elements of candidates are unique. - •
1 <= target <= 40
Approaches
Generate all possible combinations and check their sum.
def combination_sum(candidates, target):
result = []
def backtrack(start, current, remaining):
if remaining == 0:
result.append(current[:])
return
if remaining < 0:
return
for i in range(start, len(candidates)):
current.append(candidates[i])
backtrack(i, current, remaining - candidates[i])
current.pop()
backtrack(0, [], target)
return resultUse backtracking with pruning when remaining becomes negative.
def combination_sum(candidates, target):
result = []
candidates.sort()
def backtrack(start, current, remaining):
if remaining == 0:
result.append(current[:])
return
for i in range(start, len(candidates)):
if candidates[i] > remaining:
break
current.append(candidates[i])
backtrack(i, current, remaining - candidates[i])
current.pop()
backtrack(0, [], target)
return resultSame backtracking with sorting for early termination.
Diagram
def combination_sum(candidates, target):
result = []
def backtrack(start, path, remaining):
if remaining == 0:
result.append(path[:])
return
for i in range(start, len(candidates)):
if candidates[i] > remaining:
break
path.append(candidates[i])
backtrack(i, path, remaining - candidates[i])
path.pop()
candidates.sort()
backtrack(0, [], target)
return resultComplexity Comparison
| Approach | Time | Space | Description |
|---|---|---|---|
| Brute Force - All Combinations | O(n^target) | O(target) | Generate all possible combinations and check their sum. |
| Backtracking with Pruning | O(n^target) | O(target) | Use backtracking with pruning when remaining becomes negative. |
| Optimized Backtracking | O(n^target) | O(target) | Same backtracking with sorting for early termination. |
Generate all possible combinations and check their sum.
Use backtracking with pruning when remaining becomes negative.
Same backtracking with sorting for early termination.
Common Mistakes
Not sorting candidates for early termination
Using the wrong starting index in recursion
Not handling the case where a single candidate equals the target