Skip to main content

Combination Sum

LeetCode 39 | Difficulty: Medium​

Medium

Problem Description​

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.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:

Input: candidates = [2,3,6,7], target = 7
Output: [[2,2,3],[7]]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

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: []

Constraints:

- `1 <= candidates.length <= 30`

- `2 <= candidates[i] <= 40`

- All elements of `candidates` are **distinct**.

- `1 <= target <= 40`

Topics: Array, Backtracking


Approach​

Backtracking​

Explore all candidates by building solutions incrementally. At each step, choose an option, explore further, then unchoose (backtrack) to try the next option. Prune branches that can't lead to valid solutions.

When to use

Generate all combinations/permutations, or find solutions that satisfy constraints.


Solutions​

Solution 1: C# (Best: 172 ms)​

MetricValue
Runtime172 ms
Memory42.5 MB
Date2022-02-17
Solution
public class Solution {
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
IList<IList<int>> result = new List<IList<int>>();
if(candidates.Length == 0 || candidates == null) return result;
Array.Sort(candidates);
List<int> path = new List<int>();
CombinationDfs(candidates, target, 0,0, path, result);
return result;

}

private void CombinationDfs(int[] candidates, int target, int cursum,int index, List<int> path, IList<IList<int>> result)
{
if (cursum > target) return;
if (cursum == target)
{
result.Add(path.ToList());
return;
}
for (int i = index; i < candidates.Length; i++)
{
path.Add(candidates[i]);
CombinationDfs(candidates, target, cursum+candidates[i],i, path, result);
path.RemoveAt(path.Count-1);
}
}
}
πŸ“œ 1 more C# submission(s)

Submission (2018-01-21) β€” 554 ms, N/A​

public class Solution {
public IList<IList<int>> CombinationSum(int[] candidates, int target) {
List<IList<int>> dp = new List<IList<int>>();
Array.Sort(candidates);
backtrack(dp, new List<int>(), candidates, target, 0);
return dp;

}

public void backtrack(List<IList<int>> dp, List<int> templist, int[] candidates, int target, int start)
{
if (target < 0) return;
else if (target == 0)
{
dp.Add(templist.ToList());
}
else
{
for (int i = start; i < candidates.Length; i++)
{
templist.Add(candidates[i]);
backtrack(dp,templist, candidates, target-candidates[i], i);
templist.RemoveAt(templist.Count-1);
}
}

}




}

Complexity Analysis​

ApproachTimeSpace
Backtracking$O(n! or 2^n)$$O(n)$

Interview Tips​

Key Points
  • Discuss the brute force approach first, then optimize. Explain your thought process.
  • Identify pruning conditions early to avoid exploring invalid branches.