Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Hard · Free

Intro to coin change — overlapping subproblems and optimal substructure.

FresherFundamentalsData Structures & Algorithms
Model answer

Coin change DP

Min coins to make amount—overlapping subproblems (reuse sub-amounts), optimal substructure.

java
int coinChange(int[] coins, int amount) {
  int[] dp = new int[amount + 1];
  Arrays.fill(dp, amount + 1);
  dp[0] = 0;
  for (int a = 1; a <= amount; a++) {
    for (int c : coins) {
      if (c <= a) dp[a] = Math.min(dp[a], dp[a - c] + 1);
    }
  }
  return dp[amount] > amount ? -1 : dp[amount];
}
Clarify unlimited coin supply vs 0/1 knapsack variant.