39. Combination Sum
Medium NeetCode 150 ↗ Open on LeetCode Source: NeetCode 150
Quick review
- Pattern
- Backtracking
- Time
- O(N^T/M)
- Space
- O(T/M)
Standard Solution (C++)
SummaryGiven a list of distinct positive integers and a target value, find all unique combinations of numbers from the list that sum exactly to the target, where each number may be reused as many times as needed.
Approach
- Sort the candidates so we can process them in order and prune early when a candidate already exceeds the remaining target.
- Use a backtracking DFS where at each step we either include the current candidate again (staying at the same index) or skip to the next one, which naturally avoids duplicate combinations.
- Recursing with the same index allows unlimited reuse of a number, while advancing the index ensures we never revisit earlier candidates and thus never produce the same multiset twice.
- When the remaining target hits zero we record the current path; if it goes negative or we run out of candidates we prune immediately.
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
void backtrack(vector<int>& candidates, int target, int start,
vector<int>& path, vector<vector<int>>& result) {
if (target == 0) {
result.push_back(path);
return;
}
for (int i = start; i < (int)candidates.size(); ++i) {
// Sorted candidates: once one exceeds target, all remaining will too
if (candidates[i] > target) break;
path.push_back(candidates[i]);
// Pass i (not i+1) to allow reusing the same element
backtrack(candidates, target - candidates[i], i, path, result);
path.pop_back();
}
}
public:
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
sort(candidates.begin(), candidates.end());
vector<vector<int>> result;
vector<int> path;
backtrack(candidates, target, 0, path, result);
return result;
}
}; Time O(N^(T/M)) where N is the number of candidates, T is the target, and M is the smallest candidate — this bounds the depth and branching of the recursion tree Space O(T/M) for the recursion stack depth and the current path, not counting the output
A canonical reference solution. Problem statement © LeetCode — see the official link above.
My Solution (C++)
class Solution {
public:
void find(vector<vector<int>>& result, vector<int>& cur, vector<int>& candidates, int target, int idx){
if(target==0){
result.push_back(cur);
return;
}
if(target<0 || idx>=candidates.size() || candidates[idx]>target)
return;
cur.push_back(candidates[idx]);
find(result, cur, candidates, target-candidates[idx], idx);
cur.pop_back();
find(result, cur, candidates, target, idx+1);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> result;
vector<int> cur;
std::sort(candidates.begin(), candidates.end());
find(result, cur, candidates, target, 0);
return result;
}
};
// 2 ...
// 2 2 ...
// 2 3 ...
// ...
// ======= (pop back)
// 3 ...
// ...
// Time Complexity: O(N^T/M) where N is the number of candidates, T is the target, M is the minimum candidate
// Space Complexity: O(T/M) Time O(N^T/M) Space O(T/M)
My own solution and notes, as I wrote them.
▶ Run it — editable in-browser playground with a sample driver C++
Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.
C++ ▶ Runnable Auto-generated driver with sample input — unverified.
Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.