Skip to content

Commit

Permalink
Created 322-Coin-Change
Browse files Browse the repository at this point in the history
  • Loading branch information
loczek committed Jul 8, 2022
1 parent ba50ce0 commit db7a2a8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions typescript/322-Coin-Change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function coinChange(coins: number[], amount: number): number {
const dp = Array(amount + 1).fill(amount + 1);

dp[0] = 0;

for (let a = 1; a < amount + 1; a++) {
for (const c of coins) {
if (a - c >= 0) {
dp[a] = Math.min(dp[a], 1 + dp[a - c]);
}
}
}

return dp[amount] != amount + 1 ? dp[amount] : -1;
}

0 comments on commit db7a2a8

Please sign in to comment.