
Created: 2026-03-21
Updated: 2026-03-21
Given:
coins[] — denominationsamountReturn:
👉 number of combinations that make up the amount
👉 Unlimited coins of each type
[1,2] and [2,1] are the same combinationDefine: dp[i] = number of ways to make amount i
Base case:: dp[0] = 1
👉 One way to make 0: choose nothing
dp[i] += dp[i - coin]
Meaning:
ii - coin then add this coinfor coin in coins:
for i from coin → amount:
👉 Ensures combinations (not permutations)
function change(amount: number, coins: number[]): number {
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];
}
Example:
amount = 5
coins = [1,2,5]
dp = [1, 0, 0, 0, 0, 0]
dp = [1, 1, 1, 1, 1, 1]
dp[2] += dp[0] → 2
dp[3] += dp[1] → 2
dp[4] += dp[2] → 3
dp[5] += dp[3] → 3
dp = [1, 1, 2, 2, 3, 3]
dp[5] += dp[0] → 4
dp = [1, 1, 2, 2, 3, 4]
4