Skip to content

Instantly share code, notes, and snippets.

@Raghav2211
Created April 10, 2022 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raghav2211/a67d1b9d1059fc7e6bcf9488a54c7c68 to your computer and use it in GitHub Desktop.
Save Raghav2211/a67d1b9d1059fc7e6bcf9488a54c7c68 to your computer and use it in GitHub Desktop.
public static int coinChange(int[] coins, int amount, Map<Integer, Integer> map) {
if (map.get(amount) != null) return map.get(amount);
if (amount == 0) return 0;
if (amount < 0) return -1;
int minimumCoin = Integer.MAX_VALUE;
for (int coin : coins) {
int remainder = amount - coin;
int coinNeeded = coinChange(coins, remainder, map);
map.put(remainder, coinNeeded);
if (coinNeeded != -1) {
minimumCoin = Math.min(minimumCoin, coinNeeded + 1);
}
}
return minimumCoin == Integer.MAX_VALUE ? -1 : minimumCoin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment