Skip to content

Commit

Permalink
Coin Change Problem solution in JS (jainaman224#2132)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhushanaj authored and Amitsharma45 committed May 31, 2020
1 parent 8af095e commit dcfb2cb
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Coin_Change/Coin_Change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
The Coin Change problem states that given an amount N, how
many possible ways are there to make the desired change using
unlimited supply of coins of given denominations.
*/

const coinChange = (denominations, amount) => {

//make an array of length amount+1 and initialize with 0
let dp_Definition = Array.from({ length: amount + 1 }, () => 0);

//setting the base case for the definition
dp_Definition[0] = 1;

for (let i = 0; i < denominations.length; i++) {
for (let j = denominations[i]; j < amount + 1; j++) {
dp_Definition[j] += dp_Definition[j - denominations[i]]
}
}
return dp_Definition[amount];
}

// I/O , O/P Examples

console.log(coinChange([1, 5, 10, 25], 1)); //1
console.log(coinChange([1, 5, 10, 25], 2)); //1
console.log(coinChange([1, 5, 10, 25], 5)); //2
console.log(coinChange([1, 5, 10, 25], 25)); //13

0 comments on commit dcfb2cb

Please sign in to comment.