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 1272
并查集 要判断这个图是连通的 就是只有一个父节点 #include <cstdio> #include <cstring> #define maxn 100005 int f ...
- Unity3D调用第三方SDK(之一)从eclipse到Unity3D 友盟
原地址:http://www.360doc.com/content/14/0120/14/11670799_346638215.shtml 篇展示在Unity3D中调用友盟SDK的实现方法. 首先附上 ...
- APP 上传之后出现"invalid binary" 问题解决汇总
背景 5.1 号开始 App 审核开始强制支持 iPhone5,并禁止使用 UDID. 问题 上传 app 后一直处于 Invalid Binary 状态,并且收到一封邮件说 Non-public A ...
- OneAPM x 腾讯 | OneAPM 技术公开课·深圳 报名:前端性能大作战!
「 OneAPM 技术公开课」由应用性能管理第一品牌 OneAPM 发起,内容面向 IT 开发和运维人员.云集技术牛人.知名架构师.实践专家共同探讨技术热点. 11月28日,OneAPM 技术公开课第 ...
- *[topcoder]GUMIAndSongsDiv1
http://community.topcoder.com/stat?c=problem_statement&pm=12706&rd=15700 这题有意思.首先要观察到,如果选定一些 ...
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- Fetching android sdk component information
原文地址: Android Studio安装以及Fetching android sdk component information超时的解决方案 - sonyi - 博客园 http://www.c ...
- 转XMLHelper
http://www.cnblogs.com/lixyvip/archive/2009/09/16/1567929.html using System; using System.Collection ...
- 【一】 sched.h
第一个数据结构体是 task_struct ,这个数据结构被内核用来表示进程,包含其所有信息. 定义于文件 include/linux/sched.h 中,先看看其完整定义 struct task_s ...
- JavaScript DOM高级程序设计 3.-DOM2和HTML2--我要坚持到底!
由一个HTML进行说明,我就不敲了,直接copy <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" " ...