LeetCode 377. Combination Sum IV

377. Combination Sum IV

Description Submission Solutions

  • Total Accepted: 28369
  • Total Submissions: 68114
  • Difficulty: Medium
  • Contributors: Admin 

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?

Credits:
Special thanks to @pbrother for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.


【题目分析】

这是一道动态规划的题目,动态规划的难点就是如果把一个问题分解为若干的子问题。这个题目的意思是给定一个元素不重复的整数数组和一个目标值,返回数组中和值为目标值的所有组合的数目,元素的排列顺序不同是不同的组合。


【思路】

1. 刚开始想的是,先找到所有不考虑顺序的组合,然后在再计算每个组合考虑排列顺序有多少🀄️排列方式。这种办法想想都让人感到蛋疼,但问题是没有想到这是一个动态规划的题目呀。

2. 虽然知道了这是一个动态规划的题目,但是如果把一个大问题划分为若干的子问题呢,而且大问题是如何由子问题组合到一起的呢?

这是disscus里大神的回答:

Think about the recurrence relation first. How does the # of combinations of the target related to the # of combinations of numbers that are smaller than the target?

So we know that target is the sum of numbers in the array. Imagine we only need one more number to reach target, this number can be any one in the array, right? So the # of combinations of targetcomb[target] = sum(comb[target - nums[i]]), where 0 <= i < nums.length, and target >= nums[i].

In the example given, we can actually find the # of combinations of 4 with the # of combinations of 3(4 - 1), 2(4- 2) and 1(4 - 3). As a result, comb[4] = comb[4-1] + comb[4-2] + comb[4-3] = comb[3] + comb[2] + comb[1].

Then think about the base case. Since if the target is 0, there is only one way to get zero, which is using 0, we can set comb[0] = 1.

EDIT: The problem says that target is a positive integer that makes me feel it's unclear to put it in the above way. Since target == 0only happens when in the previous call, target = nums[i], we know that this is the only combination in this case, so we return 1.

Now we can come up with at least a recursive solution.

 1 public int combinationSum4(int[] nums, int target) {
 2     if (target == 0) {
 3         return 1;
 4     }
 5     int res = 0;
 6     for (int i = 0; i < nums.length; i++) {
 7         if (target >= nums[i]) {
 8             res += combinationSum4(nums, target - nums[i]);
 9         }
10     }
11     return res;
12 }

Now for a DP solution, we just need to figure out a way to store the intermediate results, to avoid the same combination sum being calculated many times. We can use an array to save those results, and check if there is already a result before calculation. We can fill the array with -1 to indicate that the result hasn't been calculated yet. 0 is not a good choice because it means there is no combination sum for the target.

 1 public int combinationSum4(int[] nums, int target) {
 2     int[] comb = new int[target + 1];
 3     comb[0] = 1;
 4     for (int i = 1; i < comb.length; i++) {
 5         for (int j = 0; j < nums.length; j++) {
 6             if (i - nums[j] >= 0) {
 7                 comb[i] += comb[i - nums[j]];
 8             }
 9         }
10     }
11     return comb[target];
12 }

 

 
posted @ 2017-02-27 09:35  Black_Knight  阅读(342)  评论(0编辑  收藏  举报