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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

`dp[i] = min(dp[i], dp[i - coins[j]] + 1);`

```class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> dp(amount + , amount + );
dp[] = ;
for (int i = ; i <= amount; ++i) {
for (int j = ; j < coins.size(); ++j) {
if (coins[j] <= i) {
dp[i] = min(dp[i], dp[i - coins[j]] + );
}
}
}
return (dp[amount] > amount) ? - : dp[amount];
}
};```

```class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
vector<int> memo(amount + , INT_MAX);
memo[] = ;
return coinChangeDFS(coins, amount, memo);
}
int coinChangeDFS(vector<int>& coins, int target, vector<int>& memo) {
if (target < ) return - ;
if (memo[target] != INT_MAX) return memo[target];
for (int i = ; i < coins.size(); ++i) {
int tmp = coinChangeDFS(coins, target - coins[i], memo);
if (tmp >= ) memo[target] = min(memo[target], tmp + );
}
return memo[target] = (memo[target] == INT_MAX) ? - : memo[target];
}
};```

```class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
unordered_map<int, int> memo;
memo[] = ;
return coinChangeDFS(coins, amount, memo);
}
int coinChangeDFS(vector<int>& coins, int target, unordered_map<int, int>& memo) {
if (target < ) return - ;
if (memo.count(target)) return memo[target];
int cur = INT_MAX;
for (int i = ; i < coins.size(); ++i) {
int tmp = coinChangeDFS(coins, target - coins[i], memo);
if (tmp >= ) cur = min(cur, tmp + );
}
return memo[target] = (cur == INT_MAX) ? - : cur;
}
};```

```class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int res = INT_MAX, n = coins.size();
sort(coins.begin(), coins.end());
helper(coins, n - , amount, , res);
return (res == INT_MAX) ? - : res;
}
void helper(vector<int>& coins, int start, int target, int cur, int& res) {
if (start < ) return;
if (target % coins[start] == ) {
res = min(res, cur + target / coins[start]);
return;
}
for (int i = target / coins[start]; i >= ; --i) {
if (cur + i >= res - ) break;
helper(coins, start - , target - i * coins[start], cur + i, res);
}
}
};```

Github 同步地址：

https://github.com/grandyang/leetcode/issues/322

Coin Change 2

9.8 Represent N Cents 美分的组成

https://leetcode.com/problems/coin-change/

https://leetcode.com/problems/coin-change/discuss/77360/C%2B%2B-O(n*amount)-time-O(amount)-space-DP-solution

https://leetcode.com/problems/coin-change/discuss/77368/*Java*-Both-iterative-and-recursive-solutions-with-explanations

LeetCode All in One 题目讲解汇总(持续更新中...)

## [LeetCode] Coin Change 硬币找零的更多相关文章

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

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

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

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

3. dp算法之硬币找零问题

题目:硬币找零 题目介绍:现在有面值1.3.5元三种硬币无限个,问组成n元的硬币的最小数目? 分析:现在假设n=10,画出状态分布图: 硬币编号 硬币面值p 1 1 2 3 3 5 编号i/n总数j ...

4. codevs 3961 硬币找零【完全背包DP/记忆化搜索】

题目描述 Description 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从银行拿回最少的硬币数,并保证能用这些硬币发工资. 我们应该 ...

5. LeetCode：柠檬水找零【860】

LeetCode:柠檬水找零[860] 题目描述 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向 ...

6. NYOJ 995 硬币找零

硬币找零 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在现实生活中,我们经常遇到硬币找零的问题,例如,在发工资时,财务人员就需要计算最少的找零硬币数,以便他们能从 ...

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

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

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

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

这是悦乐书的第331次更新,第355篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第201题(顺位题号是860).在柠檬水摊上,每杯柠檬水的价格为5美元.客户站在队列中向 ...

## 随机推荐

1. 分布式服务协调员zookeeper - 应用场景和监控

2. 深入理解CSS动画animation

× 目录 [1]定义 [2]关键帧 [3]动画属性 [4]多值 [5]API 前面的话 transition过渡是通过初始和结束两个状态之间的平滑过渡实现简单动画的:而animation则是通过关键帧 ...

3. JavaScript 垃圾回收

在公司经常会听到大牛们讨论时说道内存泄露神马的,每每都惊羡不已,最近精力主要用在了Web 开发上,读了一下<JavaScript高级程序设计>(书名很唬人,实际作者写的特别好,由浅入深)了 ...

4. 利用js取到下拉框中选择的值

现在的需求是:下拉框中要是选择加盟商让其继续选择学校,要是选择平台管理员则不需要选择学校.隐藏选择下拉列表. 选择枚举值: /// <summary> /// 平台角色 /// </ ...

5. 现有语言不支持XXX方法

史上最强大的IDE也会有bug的时候哈,今天遇到这个问题特别郁闷,百度了下,果然也有人遇到过这个问题 解决方法: 1.调用的时候参数和接口声明的参数不一致(检查修改) 2.继承接口中残留一个废弃的方法 ...

6. sql将查询的结果集一次性插入到表变量中

sql代码: declare @Subject table (--题目表变量 SubjectID int, Question nvarchar(MAX), CorrectAnswer ), Expla ...

7. pwm 占空比 频率可调的脉冲发生器

module xuanpin #(parameter N=25)(clk,clr,key_in_f,key_in_z,f_out);input clk,clr,key_in_f,key_in_z;ou ...

8. Delphi_06_Delphi_Object_Pascal_基本语法_04

这一节描述基本语法中的流程语句: 条件语句 IF语句. 选择语句 Case语句.循环语句  while/repeat/for.以及continue.break语句,还有终止程序 运行流程Exit.Ha ...

9. SQL Server导入数据时“启用标示插入”详解

在SQL Server中导入数据时,会有一个"启用标示插入"的选项,突然间懵逼了,这到底啥意思?我选与不选这个选项,结果好像没区别!不科学啊这,"存在即合理", ...

10. px-rem 一个将px转换为rem的工具

将px转换为rem的工具,github地址:https://github.com/finance-sh/px-rem 怎样转换静态文件 安装: npm install px-rem -g 然后跑下命令 ...