From f8ff28617c7f77c4ea4035736fca9af8c9e8c6b6 Mon Sep 17 00:00:00 2001 From: nttzamos Date: Sat, 15 Aug 2020 14:58:56 +0300 Subject: [PATCH 1/4] Added CoinChange Algorithm --- Dynamic-Programming/CoinChange.js | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Dynamic-Programming/CoinChange.js diff --git a/Dynamic-Programming/CoinChange.js b/Dynamic-Programming/CoinChange.js new file mode 100644 index 0000000000..0ae31deb6e --- /dev/null +++ b/Dynamic-Programming/CoinChange.js @@ -0,0 +1,56 @@ +function change(coins, amount){ + let combinations = new Array(amount + 1).fill(0) + combinations[0] = 1 + + for (let i=0; i Date: Sat, 15 Aug 2020 15:22:35 +0300 Subject: [PATCH 2/4] Minor Changes --- Dynamic-Programming/CoinChange.js | 83 ++++++++++++++++--------------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/Dynamic-Programming/CoinChange.js b/Dynamic-Programming/CoinChange.js index 0ae31deb6e..f8634e9b3b 100644 --- a/Dynamic-Programming/CoinChange.js +++ b/Dynamic-Programming/CoinChange.js @@ -1,56 +1,57 @@ -function change(coins, amount){ - let combinations = new Array(amount + 1).fill(0) - combinations[0] = 1 - - for (let i=0; i Date: Sat, 15 Aug 2020 15:27:35 +0300 Subject: [PATCH 3/4] Minor Changes --- Dynamic-Programming/CoinChange.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Dynamic-Programming/CoinChange.js b/Dynamic-Programming/CoinChange.js index f8634e9b3b..ba0c22a896 100644 --- a/Dynamic-Programming/CoinChange.js +++ b/Dynamic-Programming/CoinChange.js @@ -2,10 +2,10 @@ function change (coins, amount) { const combinations = new Array(amount + 1).fill(0) combinations[0] = 1 - for (let i = 0; i < coins.length; i++){ + for (let i = 0; i < coins.length; i++) { const coin = coins[i] - for (let j = coin; j < amount + 1; j++){ + for (let j = coin; j < amount + 1; j++) { combinations[j] += combinations[j - coin] } // Uncomment the line below to see the state of combinations for each coin @@ -15,8 +15,8 @@ function change (coins, amount) { } function minimumCoins (coins, amount) { - //minimumCoins[i] will store the minimum coins needed for amount i - let minimumCoins = new Array(amount + 1).fill(0) + // minimumCoins[i] will store the minimum coins needed for amount i + const minimumCoins = new Array(amount + 1).fill(0) minimumCoins[0] = 0 @@ -27,30 +27,30 @@ function minimumCoins (coins, amount) { for (let j = 0; j < coins.length; j++) { const coin = coins[j] if (coin <= i) { - const sub_res = minimumCoins[i - coin] - if (sub_res !== Number.MAX_SAFE_INTEGER && sub_res + 1 < minimumCoins[i]) { - minimumCoins[i] = sub_res + 1 + const subRes = minimumCoins[i - coin] + if (subRes !== Number.MAX_SAFE_INTEGER && subRes + 1 < minimumCoins[i]) { + minimumCoins[i] = subRes + 1 } } } } // Uncomment the line below to see the state of combinations for each coin - //printAmount(minimumCoins); + // printAmount(minimumCoins); return minimumCoins[amount] } // A basic print method which prints all the contents of the array function printAmount (arr) { for (let i = 0; i < arr.length; i++) { - console.log(arr[i] + " ") + console.log(arr[i] + ' ') } - console.log("\n") + console.log('\n') } function main () { const amount = 12 const coins = [2, 4, 5] - console.log('Number of combinations of getting change for ' + amount + ' is: ' + change(coins,amount)) + console.log('Number of combinations of getting change for ' + amount + ' is: ' + change(coins, amount)) console.log('Minimum number of coins required for amount :' + amount + ' is: ' + minimumCoins(coins, amount)) } From 6620487a2fe731385680f4b3290fbc4754c9a714 Mon Sep 17 00:00:00 2001 From: nttzamos Date: Sat, 15 Aug 2020 15:29:39 +0300 Subject: [PATCH 4/4] Minor Changes --- Dynamic-Programming/CoinChange.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Dynamic-Programming/CoinChange.js b/Dynamic-Programming/CoinChange.js index ba0c22a896..c7050177b5 100644 --- a/Dynamic-Programming/CoinChange.js +++ b/Dynamic-Programming/CoinChange.js @@ -8,8 +8,6 @@ function change (coins, amount) { for (let j = coin; j < amount + 1; j++) { combinations[j] += combinations[j - coin] } - // Uncomment the line below to see the state of combinations for each coin - // printAmount(combinations); } return combinations[amount] } @@ -34,19 +32,9 @@ function minimumCoins (coins, amount) { } } } - // Uncomment the line below to see the state of combinations for each coin - // printAmount(minimumCoins); return minimumCoins[amount] } -// A basic print method which prints all the contents of the array -function printAmount (arr) { - for (let i = 0; i < arr.length; i++) { - console.log(arr[i] + ' ') - } - console.log('\n') -} - function main () { const amount = 12 const coins = [2, 4, 5]