2015年9月13日星期日

LintCode: Combination Sum

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.

For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3] 
Have you met this question in a real interview? 
Yes
Example
given candidate set 2,3,6,7 and target 7
A solution set is: 
[7] 
[2, 2, 3] 
Note
  • All numbers (including target) will be positive integers.
  • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
  • The solution set must not contain duplicate combinations.
Tags Expand 



Reference:
//codes
http://yucoding.blogspot.com/2012/12/leetcode-question-16-combination-sum.html
//complexity
https://gist.github.com/daifu/5844049

DFS recursively like problem combination
Note:
1. The candidates should be ordered first to make sure output an ordered output;
2. Same number can appear multiple times;
Time: O( (n + k)! )   n ---- size of candidates   k----max repeated times for each candidates
Space: O( m )   ----- solution size
stack space: the max length of the longest combination in result.


 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
class Solution {
public:
    /**
     * @param candidates: A list of integers
     * @param target:An integer
     * @return: A list of lists of integers
     */
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        // write your code here
        vector<vector<int> > res;
        vector<int> tempres;
        sort(candidates.begin(), candidates.end());
        DFS(0, target, 0, candidates, res, tempres);
        return res;
    }
    void DFS(int start, int target, int sum, vector<int> &candidates, vector<vector<int> > &res, vector<int> &tempres){
        if(sum > target) return;
        else if(sum == target) {
            res.push_back(tempres);
            return;
        }
        else{
            for(int i = start; i < candidates.size(); i++){
                tempres.push_back(candidates[i]);
                DFS(i, target, sum + candidates[i], candidates, res, tempres);
                tempres.pop_back();
            }
        }
    }
};


Lintcode second:
add line 24 to in case that there is 0 in candidates



 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
class Solution {
public:
    /**
     * @param candidates: A list of integers
     * @param target:An integer
     * @return: A list of lists of integers
     */
    vector<vector<int> > combinationSum(vector<int> &candidates, int target) {
        // write your code here
        sort(candidates.begin(), candidates.end());
        vector<vector<int> > res;
        vector<int> temp;
        dfs(0, 0, target, candidates, temp, res);
        return res;
    }
    void dfs(int start, int sum, int target, vector<int> &candidates, vector<int> &temp, vector<vector<int> > &res){
        if(sum == target){
            res.push_back(temp);
            return;
        }
        if(sum > target) return;
        for(int i = start; i < candidates.size(); i++){
            temp.push_back(candidates[i]);
            if(candidates[i] == 0) dfs(i + 1, sum, target, candidates, temp, res);
            else dfs(i, sum + candidates[i], target, candidates, temp, res);
            temp.pop_back();
        }
    }
};

没有评论:

发表评论