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 9e90dec commit 6e8760cCopy full SHA for 6e8760c
coin-change/wogha95.js
@@ -0,0 +1,23 @@
1
+// TC: O(C * A)
2
+// SC: O(C)
3
+// C: coins.length, A: amount
4
+
5
+/**
6
+ * @param {number[]} coins
7
+ * @param {number} amount
8
+ * @return {number}
9
+ */
10
+var coinChange = function (coins, amount) {
11
+ const dp = new Array(amount + 1).fill(Number.MAX_SAFE_INTEGER);
12
+ dp[0] = 0;
13
14
+ for (let index = 1; index < amount + 1; index++) {
15
+ for (const coin of coins) {
16
+ if (index - coin >= 0) {
17
+ dp[index] = Math.min(dp[index - coin] + 1, dp[index]);
18
+ }
19
20
21
22
+ return dp[amount] === Number.MAX_SAFE_INTEGER ? -1 : dp[amount];
23
+};
0 commit comments