[LeetCode] 377. Combination Sum IV 组合之和 IV
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.
解法1:递归。按照前面I, II的思路用递归来解,会TLE,比如:OJ一个test case为[4,1,2] 32,结果是39882198,用递归需要好几秒时间。
解法2:动态规划DP来解。这道题类似于322. Coin Change ,建一个一维数组dp,dp[i]表示目标数target为i时解的个数,从1遍历到target,对于每一个数i,遍历nums数组,如果i>=x, dp[i] += dp[i - x]。比如[1,2,3] 4,当计算dp[3]时,3可以拆分为1+x,而x即为dp[2],3也可以拆分为2+x,x为dp[1],3同样可以拆为3+x,x为dp[0],把所有情况加起来就是组成3的所有解。
Java: Recursive
public int combinationSum4(int[] nums, int target) {
if (target == 0) {
return 1;
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
res += combinationSum4(nums, target - nums[i]);
}
}
return res;
}
Java:
private int[] dp;
public int combinationSum4(int[] nums, int target) {
dp = new int[target + 1];
Arrays.fill(dp, -1);
dp[0] = 1;
return helper(nums, target);
}
private int helper(int[] nums, int target) {
if (dp[target] != -1) {
return dp[target];
}
int res = 0;
for (int i = 0; i < nums.length; i++) {
if (target >= nums[i]) {
res += helper(nums, target - nums[i]);
}
}
dp[target] = res;
return res;
}
Java:
public int combinationSum4(int[] nums, int target) {
int[] comb = new int[target + 1];
comb[0] = 1;
for (int i = 1; i < comb.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i - nums[j] >= 0) {
comb[i] += comb[i - nums[j]];
}
}
}
return comb[target];
}
Java:
class Solution {
public int combinationSum4(int[] nums, int target) {
if(nums==null || nums.length==0)
return 0;
int[] dp = new int[target+1];
dp[0]=1;
for(int i=0; i<=target; i++){
for(int num: nums){
if(i+num<=target){
dp[i+num]+=dp[i];
}
}
}
return dp[target];
}
}
Python:
class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
dp = [0] * (target+1)
dp[0] = 1
nums.sort() for i in xrange(1, target+1):
for j in xrange(len(nums)):
if nums[j] <= i:
dp[i] += dp[i - nums[j]]
else:
break return dp[target]
Python:
class Solution(object):
def combinationSum4(self, nums, target):
nums, combs = sorted(nums), [1] + [0] * (target)
for i in range(target + 1):
for num in nums:
if num > i: break
if num == i: combs[i] += 1
if num < i: combs[i] += combs[i - num]
return combs[target]
C++:
class Solution {
public:
int combinationSum4(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
sort(nums.begin(), nums.end());
for (int i = 1; i <= target; ++i) {
for (int j = 0; j < nums.size() && nums[j] <= i; ++j) {
dp[i] += dp[i - nums[j]];
}
}
return dp[target];
}
};
类似题目:
[LeetCode] 322. Coin Change 硬币找零
[LeetCode] 39. Combination Sum 组合之和
[LeetCode] 40. Combination Sum II 组合之和 II
[LeetCode] 216. Combination Sum III 组合之和 III
All LeetCode Questions List 题目汇总
[LeetCode] 377. Combination Sum IV 组合之和 IV的更多相关文章
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [leetcode]40. Combination Sum II组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和 II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 40. Combination Sum II 组合之和之二
Given a collection of candidate numbers (candidates) and a target number (target), find all unique c ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- LeetCode OJ:Combination Sum II (组合之和 II)
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 377 Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 解决Android8.0系统应用打开webView报错
由于webView存在安全漏洞,谷歌从5.1开始全面禁止系统应用使用webview,使用会导致应用崩溃错误提示:Caused by: java.lang.UnsupportedOperationExc ...
- Hive中的SQL执行计划--几乎所有的SQL都有
explain SQL 会解释SQL的执行过程
- Chocolatey 方便的windows 包管理工具
windows 在包管理上一般大家都是网上下载二进制文件或者就是通过软件管家进行安装,这些对于开发人员可能就有点不是 很专业了, Chocolatey 是一个不错的windows 软件包管理工具 安装 ...
- Brexit Gym - 101490C
题目链接:Brexit vector的使用(vector存边),巧用queue,相当于Bfs /* */ # include <iostream> # include <cstdio ...
- BMP文件格式详解(BMP file format)
BMP文件格式,又称为Bitmap(位图),或是DIB(Device-Independent Device,设备无关图),是windows系统中广泛使用的图片文件格式,由于它可以不作任何变换地址保存图 ...
- 从海量数据中寻找出topK的最优算法代码
package findMinNumIncludedTopN;/** * 小顶堆 * @author TongXueQiang * @date 2016/03/09 * @since JDK 1.8 ...
- XMind配置防火墙
1.打开控制面板,找到防火墙 2. 启用Windows防火墙 3.高级设置,新建入站规则 配置部分一律“下一步”. XMind配置防火墙完成.如果还不行,相同方式设置出站规则.
- iis启动 服务无法在此时接受控制信息。 (异常来自 HRESULT:0x80070425)
问题描述:每隔一段时间应用程序池就会自动停止. 再次启动就报错:服务无法在此时接受控制信息. (异常来自 HRESULT:0x80070425) 处理办法:同时按下Win+R,运行“services. ...
- Runtime Only和Runtime + Compiler
如果你需要在客户端编译模板 (比如传入一个字符串给 template 选项,或挂载到一个元素上并以其 DOM 内部的 HTML 作为模板),就将需要加上编译器,即完整版 当使用 vue-loader ...
- 作业——11 分布式并行计算MapReduce
作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3319 1.用自己的话阐明Hadoop平台上HDFS和MapRedu ...