Skip to content

Commit aba1f66

Browse files
committed
solve: coin change
1 parent d9fd156 commit aba1f66

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

coin-change/tolluset.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* TC: O(amount * coins.length)
3+
* SC: O(amount)
4+
* */
5+
function coinChange(coins: number[], amount: number): number {
6+
if (amount === 0) {
7+
return 0;
8+
}
9+
10+
const dp = new Array(amount + 1).fill(Infinity);
11+
dp[0] = 0;
12+
13+
for (let i = 1; i <= amount; i++) {
14+
coins.forEach((coin) => {
15+
if (coin <= i) {
16+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
17+
}
18+
});
19+
}
20+
21+
return dp[amount] === Infinity ? -1 : dp[amount];
22+
}

0 commit comments

Comments
 (0)