[leetcode] 377. Combination Sum IV

441 篇文章 0 订阅
284 篇文章 0 订阅

Description

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.

分析

题目的意思是:给你一堆数字,还给你一个target,求所有可能的数字组合等于target的组合。

dp[i]表示和为i的组合方法数,dp[target]就是我们所求的内容。这个dp的推导很简单,求种类数就是:
dp[i]=dp[i]+dp[i-val] val为nums中的数字

代码

class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) {
        if(nums.empty()){
            return 0;
        }
        int n=target+1;
        vector<int> dp(n,0);
        dp[0]=1;
        for(int i=1;i<n;i++){
            for(auto val:nums){
                if(i-val>=0){
                   dp[i]+=dp[i-val]; 
                }  
            }
        }
        return dp[target];
    }
};

Python代码

最开始写代码的时候,是仿照0,1背包问题解的,即两层循环,外层是背包的物品,内层遍历价值,然后就翻车了。递推公式还是很好推的:
dp[0]=1表示什么都不选,所以组合数为1.
递推公式为:dp[i]=dp[i]+dp[i-num]

class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp =[0 for _ in range(target+1)]
        dp[0]=1
        for i in range(target+1):
            for num in nums:
                if i>=num:
                    dp[i]+=dp[i-num]
        return dp[target]

参考文献

377. Combination Sum IV-动态规划

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值