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:

  1. nums = [1, 2, 3]
  2. target = 4
  3.  
  4. The possible combination ways are:
  5. (1, 1, 1, 1)
  6. (1, 1, 2)
  7. (1, 2, 1)
  8. (1, 3)
  9. (2, 1, 1)
  10. (2, 2)
  11. (3, 1)
  12.  
  13. Note that different sequences are counted as different combinations.
  14.  
  15. 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.

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:

  1. nums = [1, 2, 3]
  2. target = 4
  3.  
  4. The possible combination ways are:
  5. (1, 1, 1, 1)
  6. (1, 1, 2)
  7. (1, 2, 1)
  8. (1, 3)
  9. (2, 1, 1)
  10. (2, 2)
  11. (3, 1)
  12.  
  13. Note that different sequences are counted as different combinations.
  14.  
  15. 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.

 

Runtime: 2 ms, faster than 80.43% of Java online submissions for Combination Sum IV.

  1. class Solution {
  2. public int combinationSum4(int[] nums, int target) {
  3. int[] dp = new int[target+1];
  4. dp[0] = 1;
  5. for(int i=0; i<dp.length; i++){
  6. for(int j=0; j<nums.length; j++){
  7. if(i >= nums[j]){
  8. dp[i] += dp[i - nums[j]];
  9. }
  10. }
  11. }
  12. return dp[target];
  13. }
  14. }

LC 377. Combination Sum IV的更多相关文章

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

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

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

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

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

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

  4. 377. Combination Sum IV 返回符合目标和的组数

    [抄题]: Given an integer array with all positive numbers and no duplicates, find the number of possibl ...

  5. 377. Combination Sum IV

    问题 Given an integer array with all positive numbers and no duplicates, find the number of possible 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——DP本质:针对结果的迭代,dp[ans] <= dp[ans-i] & dp[i] 找三者关系 思考问题的维度+1,除了数据集迭代还有考虑结果

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

  8. 377. Combination Sum IV 70. Climbing Stairs

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

  9. 377 Combination Sum IV 组合之和 IV

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

随机推荐

  1. CompletionService异步非阻塞获取并行任务执行结果

    第1部分 问题引入 <Java并发编程实践>一书6.3.5节CompletionService:Executor和BlockingQueue,有这样一段话: "如果向Execut ...

  2. impala 建表时报错,不支持中文

    1.错误信息 (1366, "Incorrect string value: '\\xE6\\x8E\\x88\\xE6\\x9D\\x83...' for column 'search' ...

  3. 了解jQuery的detach()和remove()

    jQuery中提供了两种移出一个DOM元素的方法detach()和remove(),虽然是一样的功能,但是给出两种方法,必然有它的不同之处. empty() 单独说一下 ,它删除当前元素的所有子元素, ...

  4. 生成漂亮报告的Go语言代码检查工具

    上篇文章,老司机给各位同学介绍了Go语言的静态代码测试“三板斧”以及Go语言的testing类库. “三板斧”简洁明了,但是缺点也很明显,命令行执行,命令行输出.适合研发攻城狮看,不适合交给领导过目. ...

  5. PyTorch 启程&拾遗

    1..Tensors are similar to NumPy’s ndarrays, with the addition being that Tensors can also be used on ...

  6. java线程基础巩固---如何捕获线程运行期间的异常

    对于友盟统计我想搞程序的应该无人不晓,其中对于里面用得最多的功能就是对线上的崩溃进行修复,而这些异常都是运行期的,如: 其实也就是可以对线程中出现了这种运行期异常是提供有一种捕获机制对其进行统一处理, ...

  7. Kubernetes1.16下部署Prometheus+node-exporter+Grafana+AlertManager 监控系统

    Prometheus 持久化安装 我们prometheus采用nfs挂载方式来存储数据,同时使用configMap管理配置文件.并且我们将所有的prometheus存储在kube-system #建议 ...

  8. P5657 格雷码

    思路 考场上的递归思路 每次向下递归的时候判断是左半边还是右半边即可 注意向右半边递归之后下一层序列要反转过来即可 代码 #include <cstdio> #include <al ...

  9. java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream

    扩展阅读:https://blog.csdn.net/kimylrong/article/details/50353161

  10. 如何定义搜索面板的过滤器?DevExpress WPF超easy

    DevExpress广泛应用于ECM企业内容管理. 成本管控.进程监督.生产调度,在企业/政务信息化管理中占据一席重要之地.通过DevExpress WPF Controls,您能创建有着强大互动功能 ...