Combination Sum II

Posted by Bill on October 16, 2023

Combination Sum II

Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:

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

solution

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
class Solution {
  private:
  vector<vector<int>> ret;

  public:
  void dfs(vector<int>& candidates, int target, vector<int> current,
           int begin) {
    if (target < 0) {
      return;
    } else if (target == 0) {
      ret.push_back(current);
      return;
    }
    for (int i = begin; i < candidates.size(); i++) {
      if (i > begin && candidates[i] == candidates[i - 1]) {
        continue;
      }
      current.push_back(candidates[i]);
      dfs(candidates, target - candidates[i], current, i + 1);
      current.pop_back();
    }
  }

  vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
    int sum_of_elems = 0;
    for (auto& n : candidates)
           sum_of_elems += n;
    if (sum_of_elems < target) {
      return vector<vector<int>>();
    }
    vector<int> current;
    sort(candidates.begin(), candidates.end());
    dfs(candidates, target, current, 0);
    return ret;
  }
};