Skip to content

Instantly share code, notes, and snippets.

@madhur
Created July 27, 2019 14:20
Show Gist options
  • Select an option

  • Save madhur/8fe060537d1144233fbfa3641726d5ee to your computer and use it in GitHub Desktop.

Select an option

Save madhur/8fe060537d1144233fbfa3641726d5ee to your computer and use it in GitHub Desktop.
make change2
public static int makeChange(int[] coins, int amount) {
if (coins != null && coins.length > 0 && amount >= 0)
return makeChange(coins,amount,0);
return 0;
}
public static int makeChange(int[] coins, int amount, int current_coin_index) {
int next_coin_index;
if (current_coin_index < coins.length - 1){
//If the coin index is less than the last index, increment the index.
next_coin_index = current_coin_index + 1;
}
else
{
//If the current coin index is equal to the last index, return the coin itself.
return coins[current_coin_index];
}
int res = 0;
//Logic to calculate the residual amount.
for (int i = 0; i * coins[current_coin_index] <= amount; i++) {
res += makeChange(coins,amount-i*coins[current_coin_index],next_coin_index);
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment