KatInterview

322. Coin Change

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the minimum number of coins to make a specific amount. We have an infinite supply of each coin denomination. This is a classic dynamic programming problem, often categorized as an unbounded knapsack type of problem. The core idea is to build up a solution for the target amount from the solutions for smaller amounts.

Solution

Coin Change in Python

class Solution:
    def coinChange(self, coins: List[int], amount: int) -> int:
        # Create a DP list to store the minimum coins for each amount.
        # Initialize with a value larger than any possible answer. 
        # `amount + 1` is a convenient sentinel value. `float('inf')` also works.
        dp = [amount + 1] * (amount + 1)

        # Base case: 0 coins are needed to make an amount of 0.
        dp[0] = 0

        # Iterate from amount 1 to the target amount.
        for i in range(1, amount + 1):
            # For each amount `i`, try each coin.
            for coin in coins:
                # If the coin value is less than or equal to the current amount `i`.
                if coin <= i:
                    # Update dp[i] with the minimum of its current value and
                    # the value of making the rest of the amount (i - coin) plus one coin.
                    dp[i] = min(dp[i], dp[i - coin] + 1)

        # After filling the DP table, dp[amount] holds the result.
        # If it's still the initial large value, it means `amount` cannot be formed.
        result = dp[amount]
        return result if result <= amount else -1

Coin Change in Java

class Solution {
    public int coinChange(int[] coins, int amount) {
        // Create a DP array to store the minimum number of coins for each amount.
        // Initialize with a value larger than any possible answer. 'amount + 1' is a safe sentinel value.
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, amount + 1);

        // Base case: 0 coins are needed to make up an amount of 0.
        dp[0] = 0;

        // Build the DP table from amount 1 to the target amount.
        for (int i = 1; i <= amount; i++) {
            // For each amount `i`, try every coin denomination.
            for (int coin : coins) {
                // If the coin's value is not greater than the current amount `i`,
                // it can be used.
                if (coin <= i) {
                    // The number of coins to make amount `i` is the minimum of what we have
                    // and making amount `i - coin` plus this one coin.
                    dp[i] = Math.min(dp[i], dp[i - coin] + 1);
                }
            }
        }

        // If dp[amount] is still our sentinel value, it means the amount cannot be formed.
        // Otherwise, it holds the minimum number of coins.
        // The check dp[amount] > amount handles this condition.
        return dp[amount] > amount ? -1 : dp[amount];
    }
}

Coin Change in C++

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        // Create a DP array to store the minimum number of coins for each amount.
        // We initialize it with a value larger than any possible answer.
        // amount + 1 is a safe choice, as it's impossible to use more than 'amount' coins
        // (e.g., if we only use coins of value 1).
        vector<int> dp(amount + 1, amount + 1);

        // Base case: 0 coins are needed to make up an amount of 0.
        dp[0] = 0;

        // Iterate from amount 1 up to the target amount.
        for (int i = 1; i <= amount; ++i) {
            // For each amount `i`, try each coin.
            for (int coin : coins) {
                // If the coin value is less than or equal to the current amount `i`,
                // it's a candidate for the solution.
                if (coin <= i) {
                    // Update dp[i] with the minimum of its current value and
                    // the value of making the rest of the amount (i - coin) plus one coin.
                    dp[i] = min(dp[i], dp[i - coin] + 1);
                }
            }
        }

        // After filling the DP table, dp[amount] holds the result.
        // If it's still the initial large value, it means `amount` cannot be formed.
        // So we check if dp[amount] > amount.
        return dp[amount] > amount ? -1 : dp[amount];
    }
};

Coin Change in TypeScript

function coinChange(coins: number[], amount: number): number {
    // Create a DP array to store the minimum coins for each amount.
    // Initialize with a value larger than any possible answer. `amount + 1` is a safe choice.
    const dp: number[] = new Array(amount + 1).fill(amount + 1);

    // Base case: 0 coins are needed to make an amount of 0.
    dp[0] = 0;

    // Iterate from amount 1 to the target amount.
    for (let i = 1; i <= amount; i++) {
        // For each amount `i`, try each coin.
        for (const coin of coins) {
            // If the coin value is less than or equal to the current amount `i`.
            if (coin <= i) {
                // Update dp[i] with the minimum of its current value and
                // the value of making the rest of the amount (i - coin) plus one coin.
                dp[i] = Math.min(dp[i], dp[i - coin] + 1);
            }
        }
    }

    // After filling the DP table, dp[amount] holds the result.
    // If it's still the initial large value, it means `amount` cannot be formed.
    const result = dp[amount];
    return result > amount ? -1 : result;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.