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. CUBRID学习笔记 39 net使用dataset 返回查询的数据

    using CUBRID.Data.CUBRIDClient;    namespace DataSetExample {     class Program     {         static ...

  2. ZOJ 3785 What day is that day?(今天是星期几?)

    Description 题目描述 It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days? 今天是星期六,11 + ...

  3. CSS笔记(六)链接

    参考:http://www.w3school.com.cn/css/css_link.asp 链接的四种状态 ① a:link - 普通的.未被访问的链接 ② a:visited - 用户已访问的链接 ...

  4. HDU1016 Prime Ring Problem(DFS回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. 2dx中文乱码问题

    我们的代码里面有一个bug 为了跟之前兼容的策划导表工具兼容 导表工具导出的excel全部都是ansi的 为了兼容就只能手动改成utf 8 无bom格式 后来策划嫌烦了 就让在程序段处理这个 研究了好 ...

  6. iOS - TouchID 指纹识别

    前言 NS_CLASS_AVAILABLE(10_10, 8_0) @interface LAContext : NSObject 指纹识别功能是 iPhone 5s 推出的,SDK 是 iOS 8. ...

  7. 抓包工具__Windows

    我常使用的: 1. SoftPerfect Network Protocol Analyzer 2. fiddler . fiddler2 3.

  8. hdu4588Count The Carries

    链接 去年南京邀请赛的水题,当时找规律过的,看它长得很像数位dp,试了试用数位dp能不能过,d出每位上有多少个1,然后TLE了..然后用规律优化了前4位,勉强过了. 附数位dp代码及找规律代码. #i ...

  9. mysql 循环控制

    1.使用whileDROP PROCEDURE IF EXISTS `addstudent`;DELIMITER ;;CREATE PROCEDURE `addstudent`(iNum int)BE ...

  10. 【Todo】Python字符编码学习

    Python中经常出现字符编码问题,在这里统一整理吧. 参考这篇文章:http://www.cnblogs.com/huxi/archive/2010/12/05/1897271.html 另外这个人 ...