Skip to content

Commit dee23f1

Browse files
authored
Merge pull request neetcode-gh#94 from henrhie/coin-change-javascript
add coin change solution for javascript
2 parents 948aeb6 + b2f06e6 commit dee23f1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

javascript/322-Coin-Change.js

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

0 commit comments

Comments
 (0)