Skip to content

Instantly share code, notes, and snippets.

@aditya2337
Last active March 15, 2017 06:29
Show Gist options
  • Save aditya2337/bfd8864bb98b624558e2885a64720517 to your computer and use it in GitHub Desktop.
Save aditya2337/bfd8864bb98b624558e2885a64720517 to your computer and use it in GitHub Desktop.
Tip at Mars
function giveTip(currencyFirst, currencySecond, sum){
let arr = [];
arr[0] = 0;
for(let i = 1; i <= sum; i++){
let minA, minB;
if( i < currencyFirst){
minA = currencyFirst - i;
}else{
minA = arr[i - currencyFirst];
}
if( i < currencySecond){
minB = currencySecond - i;
} else {
minB = arr [i - currencySecond]
}
arr[i] = Math.min(minA, minB);
}
return arr[sum];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment