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)的更多相关文章

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

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

  2. LeetCode 322. Coin Change

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

  3. [LeetCode] 518. Coin Change 2 硬币找零 2

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

  4. LeetCode OJ 322. Coin Change DP求解

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

  5. [LeetCode] 518. Coin Change 2 硬币找零之二

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

  6. 【LeetCode】322. Coin Change 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetc ...

  7. 【Leetcode】322. Coin Change

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

  8. leetcode:Coin Change

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

  9. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

随机推荐

  1. 让wordpress投稿作者在后台只看到自己的文章

    wordpress支持多作者撰写,让更多的人参与网站内容的创建是个不错的想法,UGC(User-generated content)使网站主题更丰富,不同的内容吸引不同的受众,一个好的网站应该多产生U ...

  2. [Firefly引擎][学习笔记一][已完结]带用户验证的聊天室

    原地址:http://bbs.9miao.com/thread-44571-1-1.html 前言:早在群里看到大鸡蛋分享他们团队的Firefly引擎,但一直没有时间去仔细看看,恰好最近需要开发一个棋 ...

  3. eclipse 安装配置maven

    1.安装maven 插件 从eclipse 3.7(indigo)之后,m2e 插件已host到eclipse.org 站点下: Since Eclipse 3.7 (Indigo), m2e is ...

  4. SaaS 公司如何应对 On-Call 挑战?

    Cloud Insight 集监控.管理.计算.协作.可视化于一身,帮助所有 IT 公司,减少在系统监控上的人力和时间成本投入,让运维工作更加高效.简单.本文系国内 ITOM 行业领军企业 OneAP ...

  5. 已授予账号 "以服务方式登录"的权限

    已授予账号.\Cliff "以服务方式登录"的权限 --------------------------------------------------- 进入服务管理器(Serv ...

  6. Linux用户空间与内核空间

    源:http://blog.csdn.net/f22jay/article/details/7925531 Linux 操作系统和驱动程序运行在内核空间,应用程序运行在用户空间,两者不能简单地使用指针 ...

  7. mysql concat和group_concat

    mysql concat(str1,str2...)连接两个字符串,(数字也是可以的,会转成字符串) MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL mys ...

  8. python学习笔记二--列表

    一.列表: 1. 任意类型对象的位置相关的有序集合. 2. 没有固定大小. 3. 对偏移量进行赋值及各种方法的调用,修改列表. 4. 列表是序列的一种. 5. 所有对字符串的序列操作对列表均适用. 二 ...

  9. Java之跳出多重循环

    在java里,想要跳出多重循环,有两种方法 1.在循环语句前设置一个标记,然后使用带有该标记的break语句跳出该循环 public static void main(String args[]) { ...

  10. hadoop博客 oschina

    http://my.oschina.net/Xiao629/blog?catalog=449279