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 ...
随机推荐
- Tkinter 的三大布局管理器 pack、grid 和 place用法汇总
学习python的tkinter免不了要对各个组件进行位置的排放与设定,常用的布局管理器有grid,pack和place.这三种均用于同一父组件下的组件布局,但是也是有区别的,先看下他们各自的含义吧. ...
- Cube中维度排序-通过在数据仓库增加列来实现排序
数据仓库增加排序列: 维度设置: 正确结果:
- Spark Mllib里的本地矩阵概念、构成(图文详解)
不多说,直接上干货! Local matrix:本地矩阵 数组Array(1,2,3,4,5,6)被重组成一个新的2行3列的矩阵. testMatrix.scala package zhouls.bi ...
- mysql in和exists性能比较和使用
in 是把外表和内表作hash 连接,而exists是对外表作loop循环,每次loop循环再对内表进行查询.一直以来认为exists比in效率高的说法是不准确的. 如果查询的两个表大小相当,那么用i ...
- idea报错:Error running $classname: Command line is too long. Shorten command line for $classname.
Command line is too long 打印的变量太长了,超过了限制,这都会报错...我只想知道idea基于什么原理会报这个错... 解决 1.按照提示修改该类的配置,选择jar manif ...
- tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091
1.tomcat6大并发出现:INFO: Maximum number of threads (200) created for connector with address null and por ...
- Spring Boot 集成 PageHelper
配置一:在 [pom.xml] 文件中引入依赖 <!-- mybatis的分页插件 --> <dependency> <groupId>com.github.pag ...
- feign容断忽略某些异常
@HystrixCommand(ignoreExceptions={ BusinessException.class, IllegalArgumentException.class, BadCrede ...
- 微信开发 config:invalid url domain
当遇到config:invalid url domain 有2种可能 1.没有配置url. 2.url配置错误.配置url如http://write.blog.csdn.NET/,就要这样配置writ ...
- 用jQuery实现jsonp跨域
跨域的安全限制都是指浏览器端来说的.服务器端是不存在跨域安全限制的,所以通过本机服务器端通过类似httpclient方式完成“跨域访问”的工作,然后在浏览器端用AJAX获取本机服务器端“跨域访问”对应 ...