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?

DP 解法: the key to solve DP problem is to think about how to create overlap, how to re-solve subproblems(怎么制造复用)

Bottom up dp:

 public class Solution {
public int combinationSum4(int[] nums, int target) {
if (nums==null || nums.length==0) return 0;
Arrays.sort(nums);
int[] dp = new int[target+1];
dp[0] = 1;
for (int i=1; i<=target; i++) {
for (int j=0; j<nums.length && nums[j]<=i; j++) {
dp[i] += dp[i-nums[j]];
}
}
return dp[target];
}
}

Better Solution(Bottom-up)不sort也成:

 public int combinationSum4(int[] nums, int target) {
int[] comb = new int[target + 1];
comb[0] = 1;
for (int i = 1; i < comb.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (i - nums[j] >= 0) {
comb[i] += comb[i - nums[j]];
}
}
}
return comb[target];
}

Follow up:

I think if there are negative numbers in the array, we must add a requirement that each number is only used one time, or either positive number or negative number should be used only one time, otherwise there would be infinite possible combinations.
For example, we are given:
{1, -1}, target = 1,
it's obvious to see as long as we choose n 1s and (n-1) -1s, it always sums up to 1, n can be any value >= 1.

Leetcode: Combination Sum IV && Summary: The Key to Solve DP的更多相关文章

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

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

  2. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. [LeetCode] Combination Sum 组合之和

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. LeetCode Combination Sum III

    原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...

  5. Combination Sum | & || & ||| & IV

    Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...

  6. LC 377. Combination Sum IV

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

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

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

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

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

  9. [LeetCode] Combination Sum II 组合之和之二

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. mysql备份恢复

    备份命令: mysqldump -u root -p --opt 数据库名 > /data/数据库文件名.sql 恢复命令: mysql -u root -p 数据库名</data/恢复的 ...

  2. delphi下TList的用法

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. 欢迎大家提问Android技术及职业生涯等问题

    博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 最近有些时间,但QQ群问的问题比较多,不能一一解答,如果有价值 ...

  4. Android项目框架升级尝鲜OkHttp

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 随着项目日趋稳定,需求不再总是变化,那么是时间来整理下项目了.先简单介绍下,本项目最初使用loop4 ...

  5. js严格模式“use strict”

    js的严格模式会放弃js中的一些不正规的写法,参考 http://www.cnblogs.com/God-Shell/p/3139329.html: 使用声明"use  strict&quo ...

  6. Oracle横向纵向汇总

    Oracle横向纵向汇总 有一张表test 如下, (NO 学生编号 ,cj 成绩) NO name KM CJ 001 张三 语文 80  001 张三 数学 86  001 张三 英语 75  0 ...

  7. Asp.Net MVC 路由

    原文链接:http://www.asp.net/learn/mvc/ 在这篇教程中,我将为你介绍每个ASP.NET MVC应用程序都具有的一个重要功能,称作ASP.NET路由(ASP.NET Rout ...

  8. asp.net批量删除XML节点失败的原因及解决办法

    今天操作XML的时候,用到了批量循环删除节点.出现了问题,即循环未结束,程序就跳出循环.搞了好久才弄明白. 解决前的代码: XmlNodeList items = xn.ChildNodes; //获 ...

  9. debian linux 下安装 netbeans(php)

    1.安装netbians依赖jdk7 ,所以第一步是 apt-get install openjdk-7-jre 2.安装netbians  通过wget http://dlc.sun.com.edg ...

  10. iOS 集成银联支付

    下载地址:https://open.unionpay.com/upload/download/Development_kit85427986.rar 其实我找了半个小时 也不知道怎么就下载好了 这个我 ...