-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombination Sum.cpp
More file actions
43 lines (39 loc) · 1.67 KB
/
Copy pathCombination Sum.cpp
File metadata and controls
43 lines (39 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Problem: Combination Sum
// Given an array of distinct integers (candidates) and a target integer,
// return all unique combinations where the chosen numbers sum to target.
// Each number can be used unlimited times.
//
// Approach:
// - Use backtracking to explore all possible combinations.
// - At each index, you have two choices:
// 1. Pick the current element and reduce the target (stay on same index since elements can be reused).
// 2. Skip the current element and move to the next index.
// - If target == 0, we found a valid combination -> add to answer.
// - If target < 0 or index exceeds array size, stop exploring.
// - No need for a set, as the approach itself avoids duplicates.
//
// Time Complexity: O(2^(t/m)) approx, where t = target and m = smallest candidate (branching factor).
// Space Complexity: O(target) recursion stack (worst case when repeatedly subtracting smallest number).
class Solution {
public:
void solve(vector<int> &candidates, int idx, int target,
vector<int> ¤t, vector<vector<int>> &ans) {
if (target == 0) {
ans.push_back(current);
return;
}
if (idx >= candidates.size() || target < 0) return;
// pick the element
current.push_back(candidates[idx]);
solve(candidates, idx, target - candidates[idx], current, ans);
current.pop_back();
// skip the element
solve(candidates, idx + 1, target, current, ans);
}
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
vector<vector<int>> ans;
vector<int> current;
solve(candidates, 0, target, current, ans);
return ans;
}
};