-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0322-coin-change.js
33 lines (30 loc) · 956 Bytes
/
0322-coin-change.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 322. Coin Change
* https://leetcode.com/problems/coin-change/
* Difficulty: Medium
*
* You are given an integer array coins representing coins of different denominations and an
* integer amount representing a total amount of money.
*
* Return 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.
*
* 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) {
const counts = new Array(amount + 1).fill(amount + 1);
counts[0] = 0;
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i - coins[j] >= 0) {
counts[i] = Math.min(counts[i], 1 + counts[i - coins[j]]);
}
}
}
return counts[amount] !== amount + 1 ? counts[amount] : -1;
};