Algorithm


Problem Name: 322. Coin Change

Problem Link: https://leetcode.com/problems/coin-change/

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return 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.

You may assume that you have an infinite number of each kind of coin.

 

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

 

Constraints:

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 231 - 1
  • 0 <= amount <= 104

 

 


 

Code Examples

#1 Code Example with C Programming

Code - C Programming


int coinChange(int* coins, int coinsSize, int amount) {
    int *dp, i, j, d, n;
    
    dp = malloc((amount + 1) * sizeof(int));
    //assert(dp);
    dp[0] = 0;
    
    for (i = 1; i  < = amount; i ++) {
        n = -1;
        for (j = 0; j  <  coinsSize; j ++) {
            if (i >= coins[j]) {
                d = i - coins[j];
                if (dp[d] != -1 && (n == -1 || n > dp[d] + 1)) {
                    n = dp[d] + 1;  // plus one of this coin
                }
            }
        }
        dp[i] = n;
    }

    n = dp[amount];
    
    free(dp);
    
    return n;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
coins = [1,2,5], amount = 11

Output

x
+
cmd
3

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public int coinChange(int[] coins, int amount) {
    Integer[] dp = new Integer[amount + 1];
    Arrays.fill(dp, amount + 1);
    dp[0] = 0;
    for (int i = 1; i  < = amount; i++) {
      for (int coin : coins) {
        if (i - coin < 0) {
          continue;
        }
        dp[i] = Math.min(dp[i], dp[i - coin] + 1);
      }
    }
    return dp[amount] == (amount + 1) ? -1 : dp[amount];
  }

  private int coinChangeMemoization(int[] coins, int amount, Integer[] memo) {
    if (amount  <  0) {
      return -1;
    }
    if (amount == 0) {
      return 0;
    }
    if (memo[amount] != null) {
      return memo[amount];
    }
    int minCount = Integer.MAX_VALUE;
    for (int coin : coins) {
      int count = coinChangeMemoization(coins, amount - coin, memo);
      if (count == -1) {
        continue;
      }
      minCount = Math.min(minCount, count + 1);
    }
    memo[amount] = minCount == Integer.MAX_VALUE ? -1 : minCount;
    return memo[amount];
  }
  
  // This approach times out due to overlapping subproblems
  private int coinChangeRecursive(int[] coins, int amount) {
    if (amount  <  0) {
      return -1;
    }
    if (amount == 0) {
      return 0;
    }
    int minCount = Integer.MAX_VALUE;
    for (int coin : coins) {
      int count = coinChangeRecursive(coins, amount - coin);
      if (count == -1) {
        continue;
      }
      minCount = Math.min(minCount, count);
    }
    return minCount == Integer.MAX_VALUE ? -1 : minCount;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
coins = [1,2,5], amount = 11

Output

x
+
cmd
3

#3 Code Example with Javascript Programming

Code - Javascript Programming


const coinChange = function(coins, amount) {
  const dp = new Array(amount + 1).fill(amount + 1)
  dp[0] = 0
  for (let i = 1; i  < = amount; i++) {
    for (let coin of coins) {
      if (coin <= i) dp[i] = Math.min(dp[i], dp[i - coin] + 1)
    }
  }
  return dp[amount] === amount + 1 ? -1 : dp[amount]
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
coins = [2], amount = 3

Output

x
+
cmd
-1

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def coinChange(self, coins, amount):
        """
        :type coins: List[int]
        :type amount: int
        :rtype: int
        """
        dp = [0] + [float('inf')] * amount
        for i in range(1, amount + 1): dp[i] = min([dp[i - c] if i - c >= 0 else float('inf') for c in coins]) + 1
        return dp[amount] if dp[amount] != float('inf') else -1
Copy The Code & Try With Live Editor

Input

x
+
cmd
coins = [2], amount = 3

Output

x
+
cmd
-1

#5 Code Example with C# Programming

Code - C# Programming


using System;

namespace LeetCode
{
    public class _0322_CoinChange
    {
        public int CoinChange(int[] coins, int amount)
        {
            var dp = new long[amount + 1];
            for (int i = 0; i  < = amount; i++)
                dp[i] = int.MaxValue;
            dp[0] = 0;

            for (int i = 1; i  < = amount; i++)
                foreach (var coin in coins)
                    if (coin <= i)
                        dp[i] = Math.Min(dp[i], dp[i - coin] + 1);

            return dp[amount] > amount ? -1 : (int)dp[amount];
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
coins = [1], amount = 0
Advertisements

Demonstration


Previous
#321 Leetcode Create Maximum Number Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#324 Leetcode Wiggle Sort II Solution in C, C++, Java, JavaScript, Python, C# Leetcode