77-216-39-40 - Combination Sum

77 - Combinations

#medium

Given two integers n and k, return all possible combinations of k numbers chosen from the range [1, n].

You may return the answer in any order.

Example 1:
Input: n = 4, k = 2
Output: [ [1,2],[1,3],[1,4],[2,3],[2,4],[3,4] ]
Explanation: There are 4 choose 2 = 6 total combinations.
Note that combinations are unordered, i.e., [1,2] and [2,1] are considered to be the same combination.

Example 2:
Input: n = 1, k = 1
Output: [ [1] ]
Explanation: There is 1 choose 1 = 1 total combination.

思路

  1. 葉子節點就我們要求的組合。
  2. 組合是無序的,重複的都不算。

Solution

class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;

    void backtracking( int n, int k, int startIndex ) {
        if ( path.size() == k ) {
            result.push_back( path );
            return;
        }

        // 水平遍歷,單層搜索 (1,2,3,4) -> (2,3,4) -> (3,4)
        for ( int i = startIndex; i <= n; i++ ) {
            path.push_back( i );  // 單層的操作
            backtracking( n, k, i + 1); // 垂直遞歸
            path.pop_back(); // 葉子節點pop出來
        }

    }


    // n個數 k個為一個組合
    vector<vector<int>> combine(int n, int k) {
        backtracking( n, k, 1);
        return result;
    }
};

216 - Combination Sum III

#medium

Find all valid combinations of k numbers that sum up to n such that the following conditions are true:

  • Only numbers 1 through 9 are used.
  • Each number is used at most once.

Return a list of all possible valid combinations. The list must not contain the same combination twice, and the combinations may be returned in any order.

Example 1:
Input: k = 3, n = 7
Output: [ [1,2,4] ]
Explanation:
1 + 2 + 4 = 7
There are no other valid combinations.

Example 2:
Input: k = 3, n = 9
Output: [ [1,2,6],[1,3,5],[2,3,4] ]
Explanation:
1 + 2 + 6 = 9
1 + 3 + 5 = 9
2 + 3 + 4 = 9
There are no other valid combinations.

Example 3:
Input: k = 4, n = 1
Output: []
Explanation: There are no valid combinations.
Using 4 different numbers in the range [1,9], the smallest sum we can get is 1+2+3+4 = 10 and since 10 > 1, there are no valid combination.

class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;

    void backtracking( int k, int n, int startIndex, int sum ) {
        if ( path.size() == k ) {
            if ( sum == n ) result.push_back( path );
        }

        for ( int i = startIndex; i <= 9; i++ ) {
            sum += i;
            path.push_back( i );
            backtracking( k, n, i + 1, sum );
            sum -= i;
            path.pop_back();
        }

    }

    vector<vector<int>> combinationSum3(int k, int n) {
        int sum = 0;
        backtracking( k, n, 1, sum );
        return result;
    }

};

39 - Combination Sum

#medium

Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.

The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.

The test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.

Example 1:
Input: candidates = [2,3,6,7], target = 7
Output: [ [2,2,3],[7] ]
Explanation:
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
7 is a candidate, and 7 = 7.
These are the only two combinations.

Example 2:
Input: candidates = [2,3,5], target = 8
Output: [ [2,2,2,2],[2,3,3],[3,5] ]

class Solution {
public:
    vector<vector<int>> result;

    void backtracking( vector<int>& candidates, int target, vector<int> one_result, int & sum, int startIndex ) {
        if ( sum == target ) {
            result.push_back( one_result );
            return;
        }

        for ( int i = startIndex; i < candidates.size(); i++ ) {
            one_result.push_back( candidates[i] );
            sum += candidates[i];
            if ( sum <= target ) backtracking( candidates, target, one_result, sum, i );
            sum -= candidates[i];
            one_result.pop_back();
        }

    }

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
        vector<int> one_result;
        int sum = 0;
        backtracking( candidates, target, one_result, sum, 0 );
        return result;
    }
};

40 - Combination Sum II

#medium

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.

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

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

去重 [1,2,5] [1,2,5]

// 這裡與39.組合總和最大的不同就是要去重了。
class Solution {
public:
    vector<int> path;
    vector<vector<int>> result;

    void backtracking( vector<int> & candidates, int target, int & sum, int startIndex, vector<bool>& used ) {
        if ( sum == target ) {
            result.push_back( path );
            return;
        }

        for ( int i = startIndex; i < candidates.size(); i++ ) {
            if (i > 0 && candidates[i] == candidates[i - 1] && used[i - 1] == false) {
                continue;
            }
            sum += candidates[i];
            path.push_back( candidates[i] );
            used[i] = true;
            if ( sum <= target )  backtracking( candidates, target, sum, i + 1, used );
            used[i] = false;
            path.pop_back();
            sum -= candidates[i];
        }

    }

    vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
        vector<bool> used(candidates.size(), false);
        sort( candidates.begin(), candidates.end() );
        int sum = 0;
        backtracking( candidates, target, sum, 0, used );
        return result;    
    }
};