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 ...
随机推荐
- SQLite数据库简介(转)
大家好,今天来介绍一下SQLite的相关知识,并结合Java实现对SQLite数据库的操作. SQLite是D.Richard Hipp用C语言编写的开源嵌入式数据库引擎.它支持大多数的SQL92标准 ...
- jquery mobile validation
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- IOS 系统API---NSJSONSerialization四个枚举什么意思
IOS 系统API---NSJSONSerialization四个枚举什么意思 NSJSONReadingMutableContainers:返回可变容器,NSMutableDictionary或NS ...
- 关于Application.Lock和Lock(obj)
http://www.cnblogs.com/yeagen/archive/2012/03/01/2375610.html 1.Application.Lock和Application.UnLock一 ...
- netstat命令查看服务器运行情况
netstat -n|grep 80出现大量time_wait 在运行netstat -n|grep 80 | awk '/^tcp/ {++S[$NF]} END {for(a in S) prin ...
- java中 正则表达式的使用
推荐使用第一种 第一种: //对接收的文件名的合法性进行验证 String fileName="127.0.0.1_01_20140428165022174.jpg"; Strin ...
- 使用Nginx+Keepalived组建高可用负载平衡Web server集群
一,首先说明一下网络拓扑结构: 1,Nginx 反向代理Server(HA): ①Nginx master:192.168.1.157 ②Nginx backup:192.168.1. ...
- 解决 IntelliJ 乱码问题
原文:解决 IntelliJ 乱码问题 汉字符在IntelliJ的控制台输出乱码.编译器在编译的时候,把汉字符编译成非UTF-8而引起乱码.我是在做Jsoup解析的时候出现的错误,其实归根结底确实编译 ...
- Hoax or what
Hoax or what 题意是询问一个动态序列的最小值和最大值. 可以用multiset来实现. #include <stdio.h> #include <set> usin ...
- 【安全组网】思科IOS设备基础应用
思科IOS有2种主要命令行模式:用户模式与特权模式 1.用户模式(user mode),当用“>”表示实在用户模式下 2.特权模式(exec mode),当用"#"表示是在特 ...