Skip to content

Commit 7ca169f

Browse files
committed
coin-change
1 parent f1f7933 commit 7ca169f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

coin-change/jun0811.js

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

0 commit comments

Comments
 (0)