Skip to content

Commit f486e1f

Browse files
committed
Coin Change
1 parent 46cebfe commit f486e1f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

coin-change/casentino.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
const dp = new Array(amount + 1).fill(amount);
3+
dp[0] = 0;
4+
for (let i = 1; i <= amount; i++) {
5+
for (let j = 0; j < coins.length; j++) {
6+
if (i - coins[j] >= 0) {
7+
dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]);
8+
}
9+
}
10+
}
11+
return dp[amount] === Number.POSITIVE_INFINITY ? -1 : dp[amount];
12+
}

0 commit comments

Comments
 (0)