12.Coin Change

You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

Example 1:
Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1


Example 2:
Input: coins = [2], amount = 3
Output: -1


Example 3:
Input: coins = [1], amount = 0
Output: 0


Example 4:
Input: coins = [1], amount = 1
Output: 1


Example 5:
Input: coins = [1], amount = 2
Output: 2

Solution: (Recursion)

class Solution
{
public:
    int res = INT_MAX;

    void countCoins(vector<int> &coins, int amount, int pos, vector<int> &cc)
    {
        if (pos >= coins.size())
        {
            return;
        }

        if (amount == 0)
        {
            int count = 0;
            for (int i = 0; i < cc.size(); i++)
            {
                count += cc[i];
            }
            res = min(res, count);
        }

        if (amount < 0)
        {
            return;
        }

        countCoins(coins, amount, pos + 1, cc);
        cc[pos]++;
        countCoins(coins, amount - coins[pos], pos, cc);
        cc[pos]--;
        return;
    }

    int coinChange(vector<int> &coins, int amount)
    {
        vector<int> cc(coins.size(), 0);

        countCoins(coins, amount, 0, cc);

        if (res == INT_MAX)
        {
            return -1;
        }

        return res;
    }
};

Solution: (DP)

class Solution
{
public:
    int coinChange(vector<int> &coins, int amount)
    {

        int n = coins.size();

        vector<vector<int>> dp(n + 1, vector<int>(amount + 1, 0));

        for (int i = 0; i <= n; i++)
        {
            dp[i][0] = 0;
        }

        for (int i = 0; i <= amount; i++)
        {
            dp[0][i] = amount + 1;
        }

        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= amount; j++)
            {
                if (coins[i - 1] <= j)
                {
                    int exclude = dp[i - 1][j];
                    int include = 1 + dp[i][j - coins[i - 1]];
                    dp[i][j] = min(exclude, include);
                }
                else
                {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }

        if (dp[n][amount] == amount + 1)
        {
            return -1;
        }

        return dp[n][amount];
    }
};

Solution : (DP Optimized space)

class Solution
{
public:
    int coinChange(vector<int> &coins, int amount)
    {

        vector<int> v(amount + 1, INT_MAX);
        v[0] = 0;

        for (int i = 1; i <= amount; i++)
        {
            for (int j = 0; j < coins.size(); j++)
            {
                if (coins[j] <= i)
                {
                    int res = v[i - coins[j]];
                    if (res != INT_MAX && res + 1 < v[i])
                    {
                        v[i] = res + 1;
                    }
                }
            }
        }
        if (v[amount] == INT_MAX)
        {
            return -1;
        }
        return v[amount];
    }
};

Time Complexity: O(no of coins * amount), Space Complexity: O(amount)

Last updated