518. Coin Change II
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 63% Topics: Array, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · javascript)
/**
* 518. Coin Change II
* https://leetcode.com/problems/coin-change-ii/
* 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 number of combinations that make up that amount. If that amount of money
* cannot be made up by any combination of the coins, return 0.
*
* You may assume that you have an infinite number of each kind of coin.
*
* The answer is guaranteed to fit into a signed 32-bit integer.
*/
/**
* @param {number} amount
* @param {number[]} coins
* @return {number}
*/
var change = function(amount, coins) {
const dp = new Array(amount + 1).fill(0);
dp[0] = 1;
for (const coin of coins) {
for (let i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
};
Solution from kamyu104/LeetCode-Solutions · MIT