Skip to main content

🟨 Combination Sum (#39)

📋 Problem Statement​

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. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

💡 Examples​

Example 1​

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]

Example 2​

Input: candidates = [2,3,5], target = 8
Output: [[2,2,2,2],[2,3,3],[3,5]]

Example 3​

Input: candidates = [2], target = 1
Output: []

🔑 Key Insights & Approach​

Core Observation: Use backtracking to explore all combinations. Allow reusing same element. Prune when sum exceeds target.

Why Backtracking?

  • Explores all valid combinations
  • O(2^target/min) time in worst case
  • Can reuse elements
  • Natural pruning when sum > target

Approaches:

  1. Backtracking with index: O(2^target/min) - optimal
  2. DP (less intuitive for this problem)

Pattern: "Backtracking - Combination with Reuse" pattern.

🐍 Solution: Python​

Approach: Backtracking​

Time Complexity: O(2^(target/min)) | Space Complexity: O(target/min)

from typing import List

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
result = []

def backtrack(start, current, total):
# Base cases
if total == target:
result.append(current[:])
return

if total > target:
return

# Explore candidates starting from 'start' index
for i in range(start, len(candidates)):
current.append(candidates[i])
# Can reuse same element, so pass 'i' not 'i+1'
backtrack(i, current, total + candidates[i])
current.pop()

backtrack(0, [], 0)
return result

Approach 2: With Sorting for Early Pruning​

Time Complexity: O(2^(target/min)) | Space Complexity: O(target/min)

from typing import List

class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort() # Sort for early pruning
result = []

def backtrack(start, current, total):
if total == target:
result.append(current[:])
return

for i in range(start, len(candidates)):
if total + candidates[i] > target:
break # Early pruning (sorted)

current.append(candidates[i])
backtrack(i, current, total + candidates[i])
current.pop()

backtrack(0, [], 0)
return result

🔵 Solution: Golang​

Approach: Backtracking​

Time Complexity: O(2^(target/min)) | Space Complexity: O(target/min)

func combinationSum(candidates []int, target int) [][]int {
result := [][]int{}

var backtrack func(start int, current []int, total int)
backtrack = func(start int, current []int, total int) {
if total == target {
temp := make([]int, len(current))
copy(temp, current)
result = append(result, temp)
return
}

if total > target {
return
}

for i := start; i < len(candidates); i++ {
current = append(current, candidates[i])
backtrack(i, current, total+candidates[i])
current = current[:len(current)-1]
}
}

backtrack(0, []int{}, 0)
return result
}

📚 References​