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 ...
随机推荐
- E. Selling Souvenirs 不会做
http://codeforces.com/contest/808/problem/E 不理解为什么dp = {cost, cnt1, cnt2}可以 而dp = {cost, cnt1, cnt2, ...
- IO(转换流、缓冲流)
第1章 转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者Output ...
- Android studio 3.1.1 找不到DDMS
先找到AndroidStudio配置的SDK路径: 在SDK的/tools/路径下[就是和配置ADB一样的路径]有个monitor.bat 的批处理文件: 鼠标连续点击两下monitor.bat这个批 ...
- edittext 设置不自动获取焦点
给父级控件 设置两个属性,可以把焦点抢夺过去,最好是没有任何事件的父级控件(本人比较喜欢在xml文件的跟布局设置,因为页面的跟布局一般情况下,是不会设置任何事件的) android:focusable ...
- excel如何显示多个独立窗口
https://blog.csdn.net/tigaobansongjiahuan8/article/details/76861084
- CentOS 7安装Docker服务详细过程
---恢复内容开始--- Docker 简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...
- ApexSQL Log中的Redo Script跟原始SQL不一致问题
最近遇到一个误更新数据的问题,使用ApexSQL Log做挖掘事务日志的时候,发现ApexSQL Log生成的Redo Script跟原始SQL是有区别的.它们并不是完全一致的.只是逻辑上等价而已.如 ...
- robotframework介绍
1.测试用例使用文本文件(TXT或者TSV文件)保存,使用制表符分隔数据.可以方便的使用任何文本编辑器,或者EXCEL编辑测试用例.也可以使用HTML格式创建用例.2.测试用例中支持变量使用,可以使用 ...
- hihoCoder #1165 : 益智游戏 (挑战赛11 B题)
题意:在一个序列中找到两个数a和b,使得a*b的因子个数最多,输出最多的因子个数. 思路:数据较多,处理会很慢.对序列中每个数字进行质数分解求因子个数,然后按照因子个数降序排列,对前50个因子最多的数 ...
- UWP开发:自动生成迷宫&自动寻路算法(3)
+ , + ];//0<=x<=12 0<=y<=24 private static Random Rd = new Random(); 首先声明mazeMap存储数据,声明了 ...