前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【DP】377. Combination Sum IV

【DP】377. Combination Sum IV

作者头像
echobingo
发布2019-06-14 10:31:30
5550
发布2019-06-14 10:31:30
举报
文章被收录于专栏:Bingo的深度学习杂货店
问题描述:

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:
代码语言:javascript
复制
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?
解题思路:
方法1:BFS(超时 TLE)

第一种想法是使用 【DP、BFS】322. Coin Change 这道题中的 BFS 方法,只不过这时 BFS 的目标是求能到达 amount 的次数,并且不使用 visited 剪枝。因此,很快拍出下面代码:

代码语言:javascript
复制
class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dis = [(0, 0)]
        layer = 0
        ans = 0
        while len(dis) > 0:
            while len(dis) > 0 and dis[0][1] == layer:
                tem = dis.pop(0)
                if tem[0] == target:
                    ans += 1
                for num in nums:
                    if tem[0] + num <= target:
                        dis.append((tem[0]+num, layer+1))
            layer += 1
        return ans

但是这种情况下,超时了。假设硬币中有 1,amount 又很大,构造这棵树的时间复杂度最坏可能达到 O(2^(amount)),这是不可接受的,因此这种想法不行。

方法2:DP(接受 AC)

如果使用动态规划的思想,就和 Leetcode 【DP】518. Coin Change 2 的解法很类似了,只不过这道题是求排列数,找硬币的题是求组合数。

在找硬币问题中分析了,由于是求组合数,所以外层循环应该是对 coins 的循环。但是这道题是是求排列数,nums 里面的数可以不同顺序的出现,因此这道题的内层循环应该是 nums,外层循环应该是不同的目标 i。

因此,类似于 【DP】518. Coin Change 2 ,可以想到如下算法:

  • 创建一个 dp1+target 数组,其中 dpi 表示目标为 i 时的排列方案数;
  • 初始化 dp[0] = 1,表示目标为 0 的时候方案数为 1,其他位置 dpi 为 0;
  • 外层循环是对不同目标 i 的循环,内层循环是对 nums 的循环;可以想到转移方程:**dp[i] = ∑ dp[i-nums[j]]**,表示:目标为 i 时的方案数 = 目标为 i-numsj 的方案数的加和。∑ 次数等于内层循环次数。
  • 最后,dp-1 就是答案。
Python3 实现:
代码语言:javascript
复制
class Solution:
    def combinationSum4(self, nums: List[int], target: int) -> int:
        dp = [1] + [0] * target
        for i in range(1, target+1):
            for num in nums:
                if i >= num:
                    dp[i] += dp[i-num]
        return dp[-1]

print(Solution().combinationSum4([2,1], 3))  # 3 (111、12、21)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.06.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题描述:
    • Example:
      • Follow up:
      • 解题思路:
        • 方法1:BFS(超时 TLE)
          • 方法2:DP(接受 AC)
            • Python3 实现:
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档