forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Apr 8, 2019
1 parent
0e05311
commit 25d11c7
Showing
3 changed files
with
153 additions
and
78 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
|
||
## 题目地址 | ||
https://leetcode.com/problems/coin-change/description/ | ||
|
||
## 题目描述 | ||
``` | ||
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: | ||
Input: coins = [1, 2, 5], amount = 11 | ||
Output: 3 | ||
Explanation: 11 = 5 + 5 + 1 | ||
Example 2: | ||
Input: coins = [2], amount = 3 | ||
Output: -1 | ||
Note: | ||
You may assume that you have an infinite number of each kind of coin. | ||
``` | ||
## 思路 | ||
|
||
可以用动态规划来写, 可以先画一个二维表,然后观察,其是否可以用一维表代替。 | ||
|
||
也可以暴力分析,我们把coin逆序排列,然后逐个取,取到刚好不大于amout,依次类推。 | ||
|
||
``` | ||
eg: 对于 [1,2,5] 组成 11 块 | ||
- 排序[5,2,1] | ||
- 取第一个5, 更新amout 为 11 - 5 = 6 (1⃣️) | ||
6 > 5 继续更新 为 6 - 5 = 1 (2⃣️) | ||
1 < 5 退出 | ||
- 取第二个2 | ||
1 < 2 退出 | ||
- 取最后一个元素,也就是1 | ||
1 === 1 更新为 1 - 1 = 0 (3⃣️) | ||
- amout 为 0 退出 | ||
因此结果是 3 | ||
``` | ||
## 关键点解析 | ||
|
||
- 动态规划 | ||
|
||
- 子问题 | ||
|
||
用dp[i] 来表示组成i块钱,需要最少的硬币数,那么 | ||
|
||
1. 第j个硬币我可以选择不拿 这个时候, 硬币数 = dp[i] | ||
|
||
2. 第j个硬币我可以选择拿 这个时候, 硬币数 = dp[i - coins[j]] + 1 | ||
|
||
- 和背包问题不同, 硬币是可以拿任意个 | ||
|
||
- 对于每一个 dp[i] 我们都选择遍历一遍 coin, 不断更新 dp[i] | ||
|
||
## 代码 | ||
```js | ||
/* | ||
* @lc app=leetcode id=322 lang=javascript | ||
* | ||
* [322] Coin Change | ||
* | ||
* https://leetcode.com/problems/coin-change/description/ | ||
* | ||
* algorithms | ||
* Medium (29.25%) | ||
* Total Accepted: 175K | ||
* Total Submissions: 591.9K | ||
* Testcase Example: '[1,2,5]\n11' | ||
* | ||
* 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: | ||
* | ||
* | ||
* Input: coins = [1, 2, 5], amount = 11 | ||
* Output: 3 | ||
* Explanation: 11 = 5 + 5 + 1 | ||
* | ||
* Example 2: | ||
* | ||
* | ||
* Input: coins = [2], amount = 3 | ||
* Output: -1 | ||
* | ||
* | ||
* Note: | ||
* You may assume that you have an infinite number of each kind of coin. | ||
* | ||
*/ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
|
||
var coinChange = function(coins, amount) { | ||
if (amount === 0) { | ||
return 0; | ||
} | ||
const dp = Array(amount + 1).fill(Number.MAX_VALUE) | ||
dp[0] = 0; | ||
for (let i = 1; i < dp.length; i++) { | ||
for (let j = 0; j < coins.length; j++) { | ||
if (i - coins[j] >= 0) { | ||
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1); | ||
} | ||
} | ||
} | ||
|
||
return dp[dp.length - 1] === Number.MAX_VALUE ? -1 : dp[dp.length - 1]; | ||
|
||
|
||
}; | ||
``` | ||
|
||
## 扩展 | ||
|
||
这是一道很简单描述的题目, 很多是否会被用到大公司的电面中。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters