Created
November 29, 2016 09:29
-
-
Save KodjoSuprem/7631d327e3f3e6ba1caa3f54b13e347e to your computer and use it in GitHub Desktop.
coin change problem / Knapsack problem
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
| var coinTypes = [25, 10, 5]; | |
| var coinNb = [8, 12, 20]; | |
| function computeChange(change,coinTypes, coinNb) { | |
| var rez = []; | |
| for (var i = 0; i < coinTypes.length; ++i) { | |
| if (coinTypes[i] === 0) continue; | |
| var coinsToGet = Math.floor(change / coinTypes[i]); | |
| if (coinsToGet > coinNb[i]) { | |
| coinsToGet = coinNb[i]; | |
| } | |
| change -= coinsToGet * coinTypes[i]; | |
| rez.push(coinsToGet); | |
| } return rez; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment