Created
September 30, 2018 05:09
-
-
Save squalvj/1d27b83931f17456e0e5582216997ad4 to your computer and use it in GitHub Desktop.
Coin Change Problem using greedy algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var makeChange = function(amount) { | |
var change = [], | |
total = 0; | |
[25, 10, 5, 1].forEach(function(coin) { | |
while (total + coin <= amount) { | |
change.push(coin); | |
total += coin; | |
} | |
}); | |
return change; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment