Leetcode: Combination Sum IV && Summary: The Key to Solve DP
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的更多相关文章
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- [LeetCode] Combination Sum 组合之和
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- LeetCode Combination Sum III
原题链接在这里:https://leetcode.com/problems/combination-sum-iii/ 题目: Find all possible combinations of k n ...
- Combination Sum | & || & ||| & IV
Combination Sum | Given a set of candidate numbers (C) and a target number (T), find all unique comb ...
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum II 组合之和之二
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- Finally 与 return
网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行?很多人都说不是,当然他们的回答是正确的,经过我试验,至少有两种情况下fina ...
- 【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
本文将给大家阐述一个因未设置系统环境变量ORACLE_SID导致ORA-12162错误的案例.希望大家有所思考. 1.获得有关ORA-12162报错信息的通用表述信息 [oracle@asdlabdb ...
- your project contains error(s),please fix them before running your application.错误总结
Android开发中的问题总是多种多样,今天我来总结一个浪费了我一个晚上的错误T-T:your project contains error(s),please fix them b ...
- 蓝牙Host Controller Interface笔记
1.概述 HCI提供了一个统一的使用蓝牙控制器(BR/EDR Controller,BR/EDR/LE Controller,LE Controller,AMP Controller等)的方法 ...
- Linq中常用的方法
这几天闲着也是闲着,就仔细的研究了一下Linq的语法,还有他的一些扩展方法的使用. 下面是一些常用的扩展方法. Aggregate 自定义的聚合计算 All 检测序列中所有元素是否都满足指定的条件 A ...
- 【Android测试】【随笔】与 “美丽说” 测试同事交流
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5405432.html 分享者简介 雪晗,3年+测试经验,现 ...
- 最大子序列和 o(n)
问题: 给定一整数序列A1, A2,... An (可能有负数),求A1~An的一个子序列Ai~Aj,使得Ai到Aj的和最大 例如:整数序列-2, 11, -4, 13, -5, 2, -5, -3, ...
- 使用多种客户端消费WCF RestFul服务(一)——服务端
RestFul风格的WCF既然作为跨平台.跨语言.跨技术的一种方式出现,并且在ASP.NET API流行起来之前还是架构的首选技术之一,那么我们就来简要的介绍一下WCF在各个平台客户端的操作. 开发工 ...
- android通过pc脚本执行sqlite3脚本
最近在调研市面上的一些android db框架,需要经常重复的输入一堆比如 adb shell cd /data/data/com.example.testandroiddb/databases sq ...
- Java学习-024-获取当前类名或方法名二三文
今天,看朋友编写程序,打印日志时,需要记录当前类的类名以及当前方法的方法名,我发现 TA 将类名或者方法名直接写死在了代码中...虽说这样可以实现记录类名和方法名,但是当有特殊情况需要修改类名或者方法 ...