We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f84dc87 commit 414efbcCopy full SHA for 414efbc
coin-change/HC-kang.ts
@@ -0,0 +1,15 @@
1
+// T.C: O(coins.length * amount)
2
+// S.C: O(amount)
3
+function coinChange(coins: number[], amount: number): number {
4
+ if (amount == 0) return 0;
5
+ const dp = new Array(amount + 1).fill(Infinity);
6
+ dp[0] = 0;
7
+ for (let i = 1; i <= amount; i++) {
8
+ for (let j = 0; j < coins.length; j++) {
9
+ if (coins[j] <= i) {
10
+ dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
11
+ }
12
13
14
+ return dp[amount] === Infinity ? -1 : dp[amount];
15
+};
0 commit comments