Coin Change DP DSA Solution - Study Chapter | QuizMaker

Coin Change explained with brute force recursion, optimized minimum coins DP, dry run, edge cases, complexity, and Python, C++, Java code.

Read
12m
Type
Chapter
Access
Free

Course

DSA Course: Interview Patterns and Problem Solving

Topic

Module 8: Dynamic Programming

Learning Outcome

After this lesson, you should be able to define dp[value] as the best answer for a smaller amount and build up to the target.

Problem Statement

Given coin denominations and an amount, return the fewest coins needed to make that amount, or -1 if impossible.

InputOutputWhy
coins = [1,2,5], amount = 11311 can be made with 5 + 5 + 1.

Brute Force Approach

Try every possible coin sequence recursively. The same remaining amounts appear again and again.

Optimized Approach

Use bottom-up DP where dp[value] stores the minimum coins needed for value. Try each coin as the last coin.

Exact Pseudocode

dp[0] = 0
dp[1..amount] = infinity
for value from 1 to amount:
  for coin in coins:
    if value >= coin:
      dp[value] = min(dp[value], dp[value - coin] + 1)
if dp[amount] is infinity:
  return -1
return dp[amount]

Reference Code

class Solution:
    def coinChange(self, coins, amount):
        impossible = amount + 1
        dp = [impossible] * (amount + 1)
        dp[0] = 0

        for value in range(1, amount + 1):
            for coin in coins:
                if value >= coin:
                    dp[value] = min(dp[value], dp[value - coin] + 1)

        return dp[amount] if dp[amount] != impossible else -1
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int impossible = amount + 1;
        vector<int> dp(amount + 1, impossible);
        dp[0] = 0;

        for (int value = 1; value <= amount; value++) {
            for (int coin : coins) {
                if (value >= coin) {
                    dp[value] = min(dp[value], dp[value - coin] + 1);
                }
            }
        }

        return dp[amount] == impossible ? -1 : dp[amount];
    }
};
class Solution {
    public int coinChange(int[] coins, int amount) {
        int impossible = amount + 1;
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, impossible);
        dp[0] = 0;

        for (int value = 1; value <= amount; value++) {
            for (int coin : coins) {
                if (value >= coin) {
                    dp[value] = Math.min(dp[value], dp[value - coin] + 1);
                }
            }
        }

        return dp[amount] == impossible ? -1 : dp[amount];
    }
}

Sample Dry Run

StepStateResult
dp[0]0 coinsBase state
dp[1]coin 1 gives 1dp[1] = 1
dp[5]coin 5 gives 1dp[5] = 1
dp[11]dp[6] + coin 5answer = 3

Complexity

MeasureValueReason
TimeO(amount * coins)Every amount tries every coin once.
SpaceO(amount)The dp array stores one answer per amount.

Edge Cases

Interview Checklist

FAQs

Why use amount + 1 as infinity?

You can never need more than amount coins when coin 1 exists, so amount + 1 is safely impossible.

Is this counting combinations?

No. This version finds the minimum number of coins.

What is the core pattern?

Minimum-value bottom-up DP.

Tags

Open on QuizMaker