-
-
Save robophil/f9c764eb448c60e8ecfbf01f8dd6f58c 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