给定不同面额的硬币(coins)和一个总金额(amount)。写一个函数来计算可以凑成总金额所需的最少的硬币个数。如果没有任何一种硬币组合方式能组成总金额,返回-1。
示例 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
示例 2:
coins = [2], amount = 3
return -1.
注意:
你可以认为每种硬币的数量是无限的。

详见:https://leetcode.com/problems/coin-change/description/

class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount+1,amount+1);
dp[0]=0;
for(int i=1;i<=amount;++i)
{
for(int j=0;j<coins.size();++j)
{
if(coins[j]<=i)
{
dp[i]=min(dp[i],dp[i-coins[j]]+1);
}
}
}
return dp[amount]>amount?-1:dp[amount];
}
};

参考:http://www.cnblogs.com/grandyang/p/5138186.html

322 Coin Change 零钱兑换的更多相关文章

  1. 322. Coin Change零钱兑换

    网址:https://leetcode.com/problems/coin-change/ 典型的动态规划问题,类比背包问题,这就是完全背包问题 问题的阶段:对数值 i 凑硬币 问题的状态:对数值 i ...

  2. Leetcode322. Coin Change零钱兑换

    给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: coins = ...

  3. LeetCode OJ 322. Coin Change DP求解

    题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...

  4. [LeetCode] 322. Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  5. leetcode@ [322] Coin Change (Dynamic Programming)

    https://leetcode.com/problems/coin-change/ You are given coins of different denominations and a tota ...

  6. 322. Coin Change

    动态规划里例题,硬币问题. p[i] = dp[i - coin[j]] + 1; 注意i < coin[j] dp[i-coin[j]]无解都要跳过. public class Solutio ...

  7. LeetCode 322. Coin Change

    原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...

  8. 用背包问题思路解决 322. Coin Change(完全背包)

    首先需要明白 0-1 背包问题中的放置表格,见 “玩转算法面试 从真题到思维全面提升算法思维” 9-5 节,本题思路类似表格纵向为:只考虑第 [0 …,… index] 种硬币(物品)表格横向为:需要 ...

  9. [LC] 322. Coin Change

    You are given coins of different denominations and a total amount of money amount. Write a function ...

随机推荐

  1. 九度oj 题目1049:字符串去特定字符

    题目1049:字符串去特定字符 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:10173 解决:4611 题目描述: 输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果. 输入: ...

  2. css & no margin & print pdf

    css & no margin & print pdf no header & no footer https://stackoverflow.com/questions/46 ...

  3. COJ 1156 Switching bulbs

    一道模拟题目 对于所有0 还是 1 我们都可以想象做均为 0 的状态 v[i]表示原来的值 但是对于原来为1的要加上其所在的值作为初始值 然后转化后 a[i] = -v[i]  , 如果原来为0 , ...

  4. FFT模板(From MG)

    #include<cstdio> #include<cmath> #include<algorithm> using namespace std; struct c ...

  5. Ubuntu 16.04清楚Dash历史记录

    1.[系统设置]->[安全和隐私]->[文件和应用]->[清除使用数据] 2.清楚播放记录 rm -v ~/.local/share/recently-used.xbel 3.清楚打 ...

  6. Eclipse修改默认包路径的起始文件夹

    一般新建的Java Project项目都是从src文件夹开始的,那么通过下面的操作可以自定义修改起始文件夹. 1.项目右键->[Properties] 如果不能修改时,可以直接删除后再添加回来.

  7. ISATAP隧道方式接入IPv6和RRAS(Windows路由与远程访问)似乎是冲突的

    在启用了RRAS的NAT服务器上设置ISATAP隧道,虽然能正常获取IPv6地址,但是IPv6网络实际是不通的...

  8. [Vue] Props Validations

    Components can specify requirements for its props, such as the types you’ve already seen. If a requi ...

  9. GRANT 授权

    sys(管理员)身份登录.创建usernamezsta_new create user zsta_new   identified by password   default tablespace Z ...

  10. Win7 系统管理员设置了系统策略_禁止进行此安装_怎么办

    系统管理员设置了系统策略,禁止进行此安装,怎么办 最佳答案 尝试方法一:   windows开始菜单,运行里面输入gpedit.msc打开组策略,   在"计算机配置"→管理模板→ ...