KatInterview

518. Coin Change II

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 number of distinct combinations of coins that sum up to a given amount. We have an infinite supply of each coin denomination. This is a classic dynamic programming problem, often categorized as an "unbounded knapsack" problem. The key is to count combinations, not permutations. For example, if we use coins {1, 2} to make an amount of 3, 1+2 and 2+1 are the same combination.

Solution

Coin Change II in Python

class Solution:
    def change(self, amount: int, coins: List[int]) -> int:
        # dp[i] will store the number of combinations to make up amount i.
        dp = [0] * (amount + 1)
        
        # Base case: there is one way to make amount 0 (using no coins).
        dp[0] = 1
        
        # Iterate over each coin. This specific loop order is crucial for combinations.
        # Iterating over amounts first would count permutations.
        for coin in coins:
            # For the current coin, update the dp table for all amounts it can contribute to.
            for j in range(coin, amount + 1):
                # The number of ways to form amount j is the sum of:
                # 1. Ways to form j without the current coin (already in dp[j]).
                # 2. Ways to form (j - coin) and then adding the current coin.
                dp[j] += dp[j - coin]
                
        return dp[amount]

Coin Change II in Java

class Solution {
    public int change(int amount, int[] coins) {
        // dp[i] will store the number of combinations to make up amount i.
        int[] dp = new int[amount + 1];
        
        // Base case: There is one way to make amount 0, which is by using no coins.
        dp[0] = 1;
        
        // Iterate through each coin. The order of loops is important.
        // Outer loop for coins ensures we are counting combinations, not permutations.
        for (int coin : coins) {
            // For each coin, update the dp array for amounts from the coin's value up to the total amount.
            for (int j = coin; j <= amount; j++) {
                // The number of ways to make amount j is increased by the number of ways
                // to make amount (j - coin). This adds the combinations that include the current coin.
                dp[j] += dp[j - coin];
            }
        }
        
        // The result is the number of ways to make the full amount.
        return dp[amount];
    }
}

Coin Change II in C++

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        // dp[i] will be storing the number of combinations to make up amount i.
        // Using unsigned int to prevent potential overflow in intermediate calculations,
        // although the problem guarantees the final answer fits in a signed 32-bit integer.
        vector<unsigned int> dp(amount + 1, 0);
        
        // Base case: there is one way to make amount 0, which is by using no coins.
        dp[0] = 1;
        
        // Iterate over each coin. This order is crucial for counting combinations.
        // If we were to iterate over amount first, we would be counting permutations.
        for (int coin : coins) {
            // For each coin, update the dp array for all amounts from coin value up to the total amount.
            for (int j = coin; j <= amount; ++j) {
                // The number of ways to make amount j is increased by the number of ways
                // to make amount (j - coin). This is because we can form a new combination
                // for amount j by adding the current coin to each combination for amount (j - coin).
                dp[j] += dp[j - coin];
            }
        }
        
        // The final answer is the number of ways to make up the total amount.
        return dp[amount];
    }
};

Coin Change II in TypeScript

function change(amount: number, coins: number[]): number {
    // dp[i] will store the number of combinations to make up amount i.
    const dp: number[] = new Array(amount + 1).fill(0);

    // Base case: There is one way to make amount 0, which is by using no coins.
    dp[0] = 1;

    // Iterate through each coin. This order is essential for counting combinations.
    // Swapping the loops would lead to counting permutations.
    for (const coin of coins) {
        // For each coin, update the dp array for all amounts from the coin's value
        // up to the target amount.
        for (let j = coin; j <= amount; j++) {
            // The number of ways to make amount j is increased by the number of ways
            // to make amount (j - coin). This accounts for combinations including the current coin.
            dp[j] += dp[j - coin];
        }
    }

    // The final result is the number of combinations for the target amount.
    return dp[amount];
};

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.