mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #473 from jackeyjia/patch-6
add js solution for coinChange
This commit is contained in:
@ -304,6 +304,26 @@ func min(a, b int) int {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Javascript:
|
||||||
|
```javascript
|
||||||
|
const coinChange = (coins, amount) => {
|
||||||
|
if(!amount) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dp = Array(amount + 1).fill(Infinity);
|
||||||
|
dp[0] = 0;
|
||||||
|
|
||||||
|
for(let i =0; i < coins.length; i++) {
|
||||||
|
for(let j = coins[i]; j <= amount; j++) {
|
||||||
|
dp[j] = Math.min(dp[j - coins[i]] + 1, dp[j]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dp[amount] === Infinity ? -1 : dp[amount];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user