Skip to content

Instantly share code, notes, and snippets.

@scriptonian
Created April 1, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptonian/00385445cb489dbfb9461906488c1cba to your computer and use it in GitHub Desktop.
Save scriptonian/00385445cb489dbfb9461906488c1cba to your computer and use it in GitHub Desktop.
Greedy Algorithms - Minimum Coin Change Problem
function MinimumCoinChange(cointypes) {
//save the coin types in coins
this.coins = cointypes;
this.numCoinTypes = this.coins.length;
}
MinimumCoinChange.prototype = {
/*
pass cent amount to this function
*/
generateCoins: function(amount) {
var finalresult = [],
totalAmount = amount;
//loop through the coin types. for each type find how many
//of the amount you can get
for (var i = 0; i < this.numCoinTypes; i++) {
//first get the value of the coin type
var currentTypeValue = this.coins[i],
//count the number of that type of coin. if there are two 25 cents
//then the count is two
typeCount = Math.floor(totalAmount / currentTypeValue);
//push the coin into result
finalresult.push(typeCount);
//update the total amount so on the next iteration we can find the right
//type count
totalAmount -= (currentTypeValue * typeCount);
}
//return the final results
return finalresult;
},
/*
display the types of coins generated
*/
displayResults: function(amount){
var results = this.generateCoins(amount);
console.log(results);
for(var i = 0; i < results.length; i++) {
console.log('There are(is) ', results[i], ' ', coinnames[i]);
}
}
};
var cointypes = [25, 10, 5, 1],
coinnames = ["quater(s)", "dime(s)", "nickle(s)", "penny(ies)"],
coinChanger = new MinimumCoinChange(cointypes);
coinChanger.displayResults(36);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment