Skip to content

Instantly share code, notes, and snippets.

@wohhie
Created July 14, 2021 23:49
Show Gist options
  • Save wohhie/0eebd9b49cb91e266e3a2dbef7a27351 to your computer and use it in GitHub Desktop.
Save wohhie/0eebd9b49cb91e266e3a2dbef7a27351 to your computer and use it in GitHub Desktop.
public static int coinChange(int[] coins, int amount){
return recursionHelper(coins, amount);
}
private static int recursionHelper(int[] coins, int remaining) {
if (remaining == 0) return 0;
if (remaining < 0) return -1;
int minCount = Integer.MAX_VALUE;
for (int coin: coins){
int count = recursionHelper(coins, remaining - coin);
if (count == - 1) continue;
minCount = Math.min(minCount, count + 1);
}
return minCount == Integer.MAX_VALUE ? - 1 : minCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment