Skip to content

Commit 8deee8d

Browse files
committed
coin-change solution (ts)
1 parent 3c35048 commit 8deee8d

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

coin-change/hi-rachel.ts

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

0 commit comments

Comments
 (0)