给定不同面额的硬币(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. Codeforces 989C - A Mist of Florescence

    传送门:http://codeforces.com/contest/989/problem/C 这是一个构造问题. 构造一张网格,网格中的字符为’A’.’B’.’C’.’D’,并且其连通块的个数分别为 ...

  2. HUST 1214 Cubic-free numbers II

    Cubic-free numbers II Time Limit: 10000ms Memory Limit: 131072KB This problem will be judged on HUST ...

  3. Eclipse不编译解决方案

    原文链接:http://blog.csdn.net/huahuagongzi99999/article/details/7719882    转来自己用 这两天Eclipse 不编译了,无论怎么更改保 ...

  4. H5存储------localStorage和sessionStorage

    web现在随着计算机的高速发展,客户端干的事情越来越多了,随着事情的增多,很多东西存储就不止在服务器了,本地存储越来越强大了(大神原谅我废话了

  5. [USACO06FEB]数字三角形

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  6. nyoj_518_取球游戏_201404161738

    取球游戏 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 今盒子里有n个小球,A.B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个 ...

  7. LVS中文站点

    http://blog.csdn.net/turkeyzhou/article/details/16980161 http://zh.linuxvirtualserver.org/

  8. 时间插件,js格式化,js某月天数,js某月最后一天日期

    //时间格式化 Date.prototype.format = function(fmt) { var o = { "M+": this.getMonth() + 1, //月份 ...

  9. fibonacci数列的题目——剑指Offer

    https://www.nowcoder.net/practice/c6c7742f5ba7442aada113136ddea0c3?tpId=13&tqId=11160&tPage= ...

  10. Qt移动应用开发(二):使用动画框架

    Qt移动应用开发(二):使用动画框架 上一篇博客介绍了怎样使用Qt的QML来对屏幕分辨率大小进行适应,其实,不同分辨率的适应是一个很棘手的问题,除了分辨率不同外,宽高比(aspect ratio)也不 ...