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?

class Solution(object):
def combinationSum4(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
nums = [1, 2, 3]
target = 4 f(4) = [1,f(3)] if 1 in nums U [2, f(2)] if 2 in nums U [3, f(1)] if 3 in nums U [4, f(0)] if 4 in nums The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
"""
dp = [1] + [0]*target
for x in range(1, target+1):
for n in nums:
if x >= n:
dp[x] += dp[x-n]
return dp[target]

377. Combination Sum IV——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果的更多相关文章

  1. LC 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  2. [LeetCode] 377. Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  3. 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV

    ▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...

  4. [LeetCode] 377. Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  5. 【LeetCode】377. Combination Sum IV 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. Leetcode 377. Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  7. 377. Combination Sum IV 70. Climbing Stairs

    back function (return number) remember the structure class Solution { int res = 0; //List<List< ...

  8. 377 Combination Sum IV 组合之和 IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. 377. Combination Sum IV

    问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...

随机推荐

  1. python_way ,day11 线程,怎么写一个多线程?,队列,生产者消费者模型,线程锁,缓存(memcache,redis)

    python11 1.多线程原理 2.怎么写一个多线程? 3.队列 4.生产者消费者模型 5.线程锁 6.缓存 memcache redis 多线程原理 def f1(arg) print(arg) ...

  2. jQuery实现的简单文字提示效果模拟title(转)

    来源 http://www.cnblogs.com/puzi0315/archive/2012/10/17/2727693.html 模拟title实现效果,可以修改文字的样式,换行等. 文件下载: ...

  3. 关于js运动的一些总结

    js运动实现,有两种.一种是速度版,另一种是时间版. 速度版是通过对速度的加减乘除,得出元素的运动数据.时间版是通过对时间进行Tween公式运算,得出元素的运动数据. 速度版运动优点:容易在运动过程中 ...

  4. 小题精炼-----初试C语言

    毫无疑问, 这个又是给新入门学C语言的小鲜肉准备的! 题解: 1. 直接两个循环就ok啦! 当初讲课的时候几乎讲过原题啦. 2. 直接 100的三次方就是1000000啦, 所以直接对某数的三次方开方 ...

  5. mysql概要(五)union

    1.union,union all;合并不像表联合那样记录之间交叉组合而是插入新记录的效果 union:有个特性是会去重复,如果两个结果出现相同一条记录会被忽略掉一条. uniion all:将不会出 ...

  6. 转:HTTP 301 跳转和302跳转的区别

    301和302 Http状态有啥区别?301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于: 301 redirect: 301 代表永久性转移(Permanently ...

  7. PHPStorm怎么修改选中的背景颜色呢?

    File -> Settings -> editor ->color&fonts->general->下拉框中的selection background-> ...

  8. (三)ubuntu学习前传—uboot常见环境变量

    1.环境变量如何参与程序运行(1)环境变量有2份,一份在Flash中,另一份在DDR中.uboot开机时一次性从Flash中读取全部环境变量到DDR中作为环境变量的初始化值,然后使用过程中都是用DDR ...

  9. 僵尸进程学习 & 进程状态列表 & Linux信号学习

    参考这篇文章: http://www.mike.org.cn/articles/treatment-of-zombie-processes-under-linux/ 在Linux进程的状态中,僵尸进程 ...

  10. MFC编程入门之六(对话框:创建对话框模板和修改对话框属性)

    本节开始为大家讲解偏向应用的知识--创建对话框.  对话框,大家应该很熟悉了,在我们常用的软件中大多都有对话框界面,例如,360安全卫士的主界面其实就是对话框,知识它做了很多美工方面的工作,将其大大美 ...