-
-
Save vinayverghese/f2fde91f83639db8d0b747cddcb6d8ed to your computer and use it in GitHub Desktop.
Given a set of coin denominators, find the minimum number of coins to give a certain amount of change.
This file contains hidden or 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
| function makeChange (amount) { | |
| var change = {}, | |
| i = 0, | |
| coins = makeChange.COINS, | |
| coin; | |
| while (amount && (coin = coins[i++])) { | |
| if (amount >= coin) { | |
| change[coin] = ~~(amount / coin); | |
| amount %= coin; | |
| } | |
| } | |
| return change; | |
| } | |
| makeChange.COINS = [100, 25, 10, 5, 1]; |
This file contains hidden or 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
| makeChange(199); // {1: 4, 10: 2, 25: 3, 100: 1} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment