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 ...
随机推荐
- hdu 4403
水水的dfs #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath& ...
- 网站404,500错误页面的处理,及500异常写入errorLog日志
1.web.xml 配置 <error-page> <error-code>404</error-code> <location>/404.jsp< ...
- C/C++语言参数传递----函数/方法 参数的指针引用传递
int m_value = 1; void func(int *p) { p = &m_value; } int main(int argc, char *argv[]) { int n = ...
- redis其他问题
如何解决redis高并发客户端频繁time out? 现在业务上每天有5亿+的请求,平时redis的操作在2K+每秒左右.到了高峰有3K+,这时候客户端就会频繁的报connect time out的异 ...
- iOS 10 使用相机及相簿闪退的问题修正
http://www.cnblogs.com/onechen/p/5935579.html
- 安装Hadoop系列 — 安装JDK-8u5
安装步骤如下: 1)下载 JDK 8 从http://www.oracle.com/technetwork/java/javasebusiness/downloads/ 选择下载JDK的最新版本 JD ...
- No bean named 'transactionManager' is defined
2016-10-20 23:27:17.771 INFO 7096 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped &quo ...
- Android Framework------之Input子系统
下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...
- Counting sheep...
Counting sheep... Description: Consider an array of sheep where some sheep may be missing from their ...
- php类 静态变量赋值 static $name="abc"
<?php class test { static $name="abc"; } echo test::$name; 1.巴斯科范式 class_statement: var ...