leetcode@ [322] Coin Change (Dynamic Programming)
https://leetcode.com/problems/coin-change/
You are given coins of different denominations and a total amount of money amount. 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.
Example 1:
coins = [1, 2, 5], amount = 11
return 3 (11 = 5 + 5 + 1)
Example 2:
coins = [2], amount = 3
return -1.
Note:
You may assume that you have an infinite number of each kind of coin.
class Solution {
public:
int dfs(int amount, vector<int>& coins, vector<int>& dp) {
if(amount == ) return ;
if(amount < ) return INT_MAX;
if(dp[amount]) return dp[amount];
int minChange = INT_MAX;
for(int i=;i<coins.size();++i) {
int eachChange = dfs(amount-coins[i], coins, dp);
if(eachChange == INT_MAX) minChange = min(minChange, INT_MAX);
else minChange = min(minChange, eachChange + );
}
dp[amount] = minChange;
return dp[amount];
}
int coinChange(vector<int>& coins, int amount) {
int n = coins.size();
if(n == && amount > ) return -;
if(n == && amount == ) return ;
vector<int> dp(amount+, );
dp[amount] = dfs(amount, coins, dp);
if(dp[amount] == INT_MAX) return -;
else return dp[amount];
}
};
leetcode@ [322] Coin Change (Dynamic Programming)的更多相关文章
- [LeetCode] 322. Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- LeetCode 322. Coin Change
原题 You are given coins of different denominations and a total amount of money amount. Write a functi ...
- [LeetCode] 518. Coin Change 2 硬币找零 2
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- [LeetCode] 518. Coin Change 2 硬币找零之二
You are given coins of different denominations and a total amount of money. Write a function to comp ...
- 【LeetCode】322. Coin Change 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...
- 【Leetcode】322. Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode:Coin Change
You are given coins of different denominations and a total amount of money amount. Write a function ...
- leetcode@ [134] Gas station (Dynamic Programming)
https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...
随机推荐
- ADO.net--杂七杂八(一)
private void BtnConnectDataBase_Click(object sender, RoutedEventArgs e) { string connectionString = ...
- Android名词解释
System Bars.Status Bar.Navigation Bar System Bars-->the Status bars and Navigation bars.
- 咦,为DJANGO的ORM的QUERYSET增加数据列的样码,很好用哟
这个我真的没有查资料,是通过直觉和经验弄出来的,哈哈,感觉用深一点好. 这样在模板输出时,就更好控制啦.. if self.kwargs: if self.kwargs.has_key('search ...
- iOS利用HealthKit框架从健康app中获取步数信息
微信和QQ的每日步数最近十分火爆,我就想为自己写的项目中添加一个显示每日步数的功能,上网一搜好像并有相关的详细资料,自己动手丰衣足食. 统计步数信息并不需要我们自己去实现,iOS自带的健康app已经为 ...
- Struts 2 + Spring2.5 + Hibernate3整合例子
一.效果 1. 2. 二.结构 1. 2.用到jar包 antlr-2.7.6.jaraspectjrt.jaraspectjweaver.jarc3p0-0.9.1.jarcglib-nodep-2 ...
- ANDROID_MARS学习笔记_S01_012_SeekBar
1.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns: ...
- 126. Word Ladder II
题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...
- 数据段、代码段、堆栈段、BSS段
在linux中,进程在内存中一般会分为5个段,用来存放从磁盘载入的程序代码,等. 这五个段分别是: BSS段: 通常用来存放程序中未初始化的全局变量的一块内存区域.属于静态内存分配. 问题:全局变量不 ...
- php/ java/asp.net
php大型网站用得多 企业级开发 java/asp.net用得多 这个很好理解 php 执行效率好 可塑性强 接近底层 java asp.net 封装了更多的东西,开发企业级业务 效率更高, 但是高性 ...
- 50个非常有用的PHP工具
PHP是使用最为广泛的开源服务器端脚本语言之一,当然PHP并不是速度最快的,但它却是最常用的脚本语言.这里有50个有益的PHP工具,可以大大提高你的编程工作: 调试工具 Webgrind Xdebug ...