Created
April 10, 2022 15:28
-
-
Save Raghav2211/a67d1b9d1059fc7e6bcf9488a54c7c68 to your computer and use it in GitHub Desktop.
This file contains hidden or 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, 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