Combination Sum I & II: link

Combination Sum III

Problem

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.

Ensure that numbers within the set are sorted in ascending order.

Example 1:

Input: k = 3, n = 7
Output:
[[1,2,4]]

Example 2:

Input: k = 3, n = 9
Output:
[[1,2,6], [1,3,5], [2,3,4]]

Note

思路和Combination Sum II一样,用DFS递归求解。
加一个参数count = kcount每当有新的数i加入计算集合cur则减一;同时,target,也就是给定的n,也要减少i
count0时,集合里就有k个数了。此时,若target也正好减小为0,说明当前集合pre是正解,pre加入res数组。

两个无法得到正解的情况是:
count0,而target不为0时,当然已经无法得到正解,return
count不为0target却已经小于等于0的情况下,此时仍要加入其它数以令count0,而要加入的数都是19的正整数,所以已无法满足令target0的条件,return

Solution

public class Solution {
    List<List<Integer>> res = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        helper(1, k, n, new ArrayList<Integer>());
        return res;
    }
    public void helper(int start, int count, int target, List<Integer> pre) {
        if (count == 0) {
            if (target == 0) res.add(pre);
            else return;
        }
        else {
            if (target <= 0) return;
            if (target > 0) {
                for (int i = start; i <= 9; i++) {
                    List<Integer> cur = new ArrayList<Integer> (pre);
                    cur.add(i);
                    helper(i+1, count-1, target-i, cur);
                }
            }
        }
    }
}

Combination Sum IV

Problem:

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

nums = [1, 2, 3]
target = 4

The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)

Note that different sequences are counted as different combinations.

Therefore the output is 7.

Follow up:

What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

Solution

DP method

public class Solution {
    public int combinationSum4(int[] nums, int target) {
        Arrays.sort(nums);
        int[] dp = new int[target+1];
        for (int i = 1; i <= target; i++) {
            for (int num: nums) {
                if (num == i) dp[i]++;
                else if (num < i) dp[i] += dp[i-num];
                else break;
            }
        }
        return dp[target];
    }
}

Optimized DP

public class Solution {
    public int backPackVI(int[] nums, int target) {
        int[] dp = new int[target+1];
        Arrays.sort(nums);
        dp[0] = 1;
        for (int i = 1; i <= target; i++) {
            for (int num: nums) {
                if (num <= i) dp[i] += dp[i-num];
            }
        }
        return dp[target];
    }
}

Another DP

public class Solution {
    public int backPackVI(int[] nums, int target) {
        int[] dp = new int[target+1];
        Arrays.fill(dp, -1);
        Arrays.sort(nums);
        return helper(nums, dp, target);
    }
    
    int helper(int[] nums, int[] dp, int target){
        if (dp[target] >= 0) return dp[target];
        dp[target] = 0;
        for (int i = 0; i < nums.length; i++){
            if (target > nums[i]) dp[target] += helper(nums, dp, target-nums[i]);
            else if (target == nums[i]) {
                dp[target]++;
                break;
            }
        }
        return dp[target];
    }
}

DFS: Exceeded time limit

public class Solution {
    int count = 0;
    public int backPackVI(int[] nums, int target) {
        //int count = 0;
        int sum = 0;
        dfs(nums, target, sum);
        return count;
    }
    
    void dfs(int[] nums, int target, int sum){
        if (sum > target) return;
        else if (sum == target) {
            count++;
        }
        for (int i = 0; i < nums.length; i++) {
            dfs(nums, target, sum+nums[i]);
        }
    }
}

linspiration
161 声望53 粉丝