Created
July 14, 2021 23:49
-
-
Save wohhie/0eebd9b49cb91e266e3a2dbef7a27351 to your computer and use it in GitHub Desktop.
This file contains 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
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