40 Combination Sum II

Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8, A solution set is:

[
  [1, 7],
  [1, 2, 5],
  [2, 6],
  [1, 1, 6]
]

Solution

class Solution {
public:
    int sum(vector<int>& nums) {
        int res = 0, n = nums.size();
        if ( n == 0 ) return res;
        for (int i = 0; i <= n-1; i++ ) res += nums[i];
        return res;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        int n = candidates.size();
        vector<vector<int>> res;
        vector<vector<int>> tmp_res1;
        vector<int> tmp;
        tmp_res1.push_back(tmp);
        sort(candidates.begin(), candidates.begin()+n);
        int i = 0, ni, icount;
        while ( i <= n-1 ) {
            ni = candidates[i];
            icount = 0;
            while ( i <= n-1 and candidates[i] == ni ) {icount += 1; i += 1;}
            int nres = tmp_res1.size();
            vector<vector<int>> tmp_res2;
            for ( int j = 0; j <= nres-1; j++ ) {
                tmp = tmp_res1[j];
                int s = sum(tmp);
                if ( s == target ) {res.push_back(tmp); continue;}
                tmp_res2.push_back(tmp);
                for ( int k = 1; k <= icount; k++ ) {
                    if ( k*ni + s > target ) break;
                    tmp.push_back(ni);
                    tmp_res2.push_back(tmp);
                }
            }
            tmp_res1 = tmp_res2;
        }
        // note to deal with the residual vectors!
        int nres = tmp_res1.size();
        for ( int j = 0; j <= nres-1; j++ ) {
            tmp = tmp_res1[j];
            int s = sum(tmp);
            if ( s == target ) {res.push_back(tmp); continue;}
        }
        return res;
    }
};

note: pay attention to a lot of details when implementing.

Recursive Solution

class Solution {
public:
    void help(vector<int>& candidates, int i0, int target, vector<int> curr, vector<vector<int>>& res) {
        if ( target == 0 ) { res.push_back(curr); return; }
        int n = candidates.size();
        if ( i0 == n ) return;
        int inext = i0, num = candidates[i0], icount = 0;
        while ( inext < n and candidates[inext] == num ) inext++;
        for ( int i = 0; i <= inext - i0; i++) {
            help(candidates, inext, target, curr, res);
            curr.push_back(num);
            target -= num;
            if ( target < 0 ) break;
        }
        return;
    }
    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        sort(candidates.begin(), candidates.end());
        vector<int> curr;
        vector<vector<int>> res;
        help(candidates, 0, target, curr, res);
        return res;
    }
};

results matching ""

    No results matching ""