From 32525d3259d8a5082af7e68c6662376d7112df31 Mon Sep 17 00:00:00 2001 From: Qi Jia <13632059+jackeyjia@users.noreply.github.com> Date: Tue, 6 Jul 2021 21:36:07 -0700 Subject: [PATCH] add js solution for coinChange --- problems/0322.零钱兑换.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index cf537088..302ae789 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -304,6 +304,26 @@ func min(a, b int) int { ``` +Javascript: +```javascript +const coinChange = (coins, amount) => { + if(!amount) { + return 0; + } + + let dp = Array(amount + 1).fill(Infinity); + dp[0] = 0; + + for(let i =0; i < coins.length; i++) { + for(let j = coins[i]; j <= amount; j++) { + dp[j] = Math.min(dp[j - coins[i]] + 1, dp[j]); + } + } + + return dp[amount] === Infinity ? -1 : dp[amount]; +} +``` + -----------------------