This problem looks busy at first, but the accepted solution is built around one steady invariant. For 40. Combination Sum II, the solution in this repository is mainly a backtracking solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: backtracking, greedy.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack, used.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 12 ms, faster than 51.64% of C++ online submissions for Combination Sum II.
02//Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Combination Sum II.
03class Solution {
04public:
05 int tgt;
06
07 void backtrack(vector<vector<int>>& combs, vector<int>& comb, vector<int>& candidates, vector<bool>& used, int start){
08 int cursum = accumulate(comb.begin(), comb.end(), 0);
09 if(cursum > tgt){
10 }else if(cursum == tgt){
11 combs.push_back(comb);
12 }else{
13 for(int i = start; i < candidates.size(); i++){
14 if(used[i]) continue;
15 //if i-1 not used and we use i now, then i acts just as i-1
16 if(i > 0 && candidates[i] == candidates[i-1] && !used[i-1]) continue;
17 comb.push_back(candidates[i]);
18 used[i] = true;
19 backtrack(combs, comb, candidates, used, i+1);
20 comb.pop_back();
21 used[i] = false;
22 }
23 }
24 };
25
26 vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
27 int N = candidates.size();
28 vector<vector<int>> combs;
29 vector<int> comb;
30 vector<bool> used(N, false);
31 tgt = target;
32
33 sort(candidates.begin(), candidates.end());
34
35 backtrack(combs, comb, candidates, used, 0);
36
37 return combs;
38 }
39};
Cost