leetcode-21-knapsack
322. Coin Change
Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of
money cannot be made up by any combination of the coins, return -1.You may assume that you have an infinite
number of each kind of coin.

解题思路:
比较典型的背包问题。这里f[i]存储金额为i时使用硬币的最小数目,c[i]是硬币的价值,w[i]都为1,表示使用一个这样的硬币。
状态转移方程:f[i] = min(f[i], f[i-coins[j]+1),针对i >= coins[j]。
注意:数组要初始化。。f[i]都设为amount+1(作为一个最大值)。f[0]=0。
int coinChange(vector<int>& coins, int amount) {
if (amount == 0)
return 0;
int f[amount + 1];
int i, j;
// initialization
f[0] = 0;
for (i = 0; i <= amount; i++) {
// initialization
if (i > 0)
f[i] = amount + 1;
for (j = 0; j < coins.size(); j++) {
if (i >= coins[j]) {
if (f[i - coins[j]] + 1 < f[i]) {
f[i] = f[i - coins[j]] + 1;
}
}
}
}
// if amount can not be reached, f[amount] = amount+1
return f[amount] > amount ? -1:f[amount];
}
416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

解题思路:
首先,如果所有数目的和是奇数,那么是false。然后我们看一下sum /= 2是否可以得到。状态转移方程:
dp[j] = max(dp[j], dp[j-nums[i] + nums[i])。初始化dp[0] = 0, dp[i] = sum+1。
如果说几个数相加可以得到sum,则另外几个相加也可以得到sum。因此只要看dp[sum]是否为sum即可。
bool canPartition(vector<int>& nums) {
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
sum += nums[i];
}
if (sum % 2 != 0) return false;
sum /= 2;
int dp[sum + 1] = {sum + 1};
dp[0] = 0;
for (int i = 0; i < nums.size(); i++) {
for (int j = sum; j >= nums[i]; j--) {
if (dp[j - nums[i]] + nums[i] > dp[j])
dp[j] = dp[j - nums[i]] + nums[i];
}
}
return dp[sum] == sum;
}
leetcode-21-knapsack的更多相关文章
- [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- Java实现 LeetCode 21 合并两个有序链表
21. 合并两个有序链表 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1 ...
- LeetCode 21 Merge Two Sorted Lists (有序两个链表整合)
题目链接 https://leetcode.com/problems/merge-two-sorted-lists/?tab=Description Problem: 已知两个有序链表(链表中的数 ...
- LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...
- LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- <每日 1 OJ> -LeetCode 21. 合并两个有序链表
题目: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4输出:1->1-> ...
- [LeetCode] 21. Merge Two Sorted Lists 合并有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [LeetCode]21. 合并两个有序链表(递归)
题目 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-> ...
- LeetCode 21 -- Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- 分布式Job系统Hangfire
开源分布式Job系统,调度与业务分离-如何创建一个计划HttpJob任务 项目介绍: Hangfire:是一个开源的job调度系统,支持分布式JOB!! Hangfire.HttpJob 是我针对 ...
- cmd下一些常用的命令
systeminfo 列出主机名,内存等一些系统信息 tasklist 显示进程信息 (tasklist | finstr eclipse)
- asp.net 多语言 在IIS7.5发布出现找不到资源文件
我也遇到这个问题,纠结了半天, 最后把资源文件的属性改为:内容 就可以了. 见:http://q.cnblogs.com/q/60443/
- MapReduce作业的执行流程
MapReduce任务执行总流程 一个MapReduce作业的执行流程是:代码编写 -> 作业配置 -> 作业提交 -> Map任务的分配和执行 -> 处理中间结果 -> ...
- 数据绑定以及Container.DataItem几种方式与用法分析
灵活的运用数据绑定操作 绑定到简单属性:<%#UserName%> 绑定到集合:<asp:ListBox id="ListBox1" ...
- 《移动Web前端高效开发实战》笔记1——静态布局在移动端上的自适应
1.整体缩放 整体缩放可以用在营销活动页,营销活动可能因为设计美观需求必须使用背景图片而非背景色,因此需要考虑背景图适应屏幕大小.开发者可以用320像素的宽度作为基础宽度(高度可以固定),然后通过计算 ...
- ArcGIS for Server 10.3.X 新型紧凑型缓存的解读和应用
早在2010年年底,牛魔王中王在其博客空间牛魔王的作坊中对ArcGIS 10中推出的紧凑型缓存格式进行了详细的解读,详见<ArcGIS 切片缓存紧凑文件格式分析与使用>.紧随着的4年时间里 ...
- 使用java来压缩图片
使用java来压缩图片,简单几句,清清爽爽 使用0.3的压缩比得到的结果如下(从2.8M压缩到268K,且图片的清晰度看不出明显差别): package carlspringtest; import ...
- 转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据
string username = this.textBox1.Text; string password = this.textBox2.Text; string AA = HttpUtility. ...
- Array - RemoveDuplicatesfromSortedArray
/** * 无额外空间,只要前n个是不重复的就行,不需要修改后面的数字 * @param nums 已排序的数组 * @return 去除重复数字后的长度 */ public int removeDu ...