Problem Statement:
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 <= 121 <= coins[i] <= 231 - 10 <= amount <= 104
Approach:
dp[x] = minimum coinsrequired to make amountx.- Initialize
dpwithInfinity(unreachable state) exceptdp[0] = 0(0 coins for amount 0). - Transition:
- For each amount rem from 1 → amount:
- For each coin
c: Ifrem - c ≥ 0, then: dp[rem] = min(dp[rem], 1 + dp[rem - c])
- For each coin
- For each amount rem from 1 → amount:
- If dp[amount] is still
Infinity, return -1(not possible). - Otherwise,
return dp[amount].
Time & Space Complexity:
Time Complexity: O(n x amount)
Space Complexity: O(amount)
Dry Run
Input: coins = [1, 2, 5], amount = 11
Step 0: Start Function coinChange([1,2,5], 11) n = 3 dp = [0, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞] rem = 1: - coin=1 → remainingAmount=0 → dp[1] = min(∞, 1+dp[0]) = 1 - coin=2 → remainingAmount=-1 (skip) - coin=5 → remainingAmount=-4 (skip) dp = [0, 1, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞] rem = 2: - coin=1 → remAmt=1 → dp[2] = min(∞, 1+dp[1]=2) = 2 - coin=2 → remAmt=0 → dp[2] = min(2, 1+dp[0]=1) = 1 - coin=5 → remAmt=-3 (skip) dp = [0, 1, 1, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞] rem = 3: - coin=1 → remAmt=2 → dp[3] = min(∞, 1+dp[2]=2) = 2 - coin=2 → remAmt=1 → dp[3] = min(2, 1+dp[1]=2) = 2 - coin=5 → remAmt=-2 (skip) dp = [0, 1, 1, 2, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞] rem = 4: - coin=1 → remAmt=3 → dp[4] = min(∞, 1+dp[3]=3) = 3 - coin=2 → remAmt=2 → dp[4] = min(3, 1+dp[2]=2) = 2 - coin=5 → remAmt=-1 (skip) dp = [0, 1, 1, 2, 2, ∞, ∞, ∞, ∞, ∞, ∞, ∞] rem = 5: - coin=1 → remAmt=4 → dp[5] = min(∞, 1+dp[4]=3) = 3 - coin=2 → remAmt=3 → dp[5] = min(3, 1+dp[3]=3) = 3 - coin=5 → remAmt=0 → dp[5] = min(3, 1+dp[0]=1) = 1 dp = [0, 1, 1, 2, 2, 1, ∞, ∞, ∞, ∞, ∞, ∞] rem = 6: - coin=1 → remAmt=5 → dp[6] = 1+dp[5]=2 - coin=2 → remAmt=4 → dp[6] = min(2, 1+dp[4]=3) = 2 - coin=5 → remAmt=1 → dp[6] = min(2, 1+dp[1]=2) = 2 dp = [0, 1, 1, 2, 2, 1, 2, ∞, ∞, ∞, ∞, ∞] rem = 7: - coin=1 → remAmt=6 → dp[7] = 1+dp[6]=3 - coin=2 → remAmt=5 → dp[7] = min(3, 1+dp[5]=2) = 2 - coin=5 → remAmt=2 → dp[7] = min(2, 1+dp[2]=2) = 2 dp = [0, 1, 1, 2, 2, 1, 2, 2, ∞, ∞, ∞, ∞] rem = 8: - coin=1 → remAmt=7 → dp[8] = 1+dp[7]=3 - coin=2 → remAmt=6 → dp[8] = min(3, 1+dp[6]=3) = 3 - coin=5 → remAmt=3 → dp[8] = min(3, 1+dp[3]=3) = 3 dp = [0, 1, 1, 2, 2, 1, 2, 2, 3, ∞, ∞, ∞] rem = 9: - coin=1 → remAmt=8 → dp[9] = 1+dp[8]=4 - coin=2 → remAmt=7 → dp[9] = min(4, 1+dp[7]=3) = 3 - coin=5 → remAmt=4 → dp[9] = min(3, 1+dp[4]=3) = 3 dp = [0, 1, 1, 2, 2, 1, 2, 2, 3, 3, ∞, ∞] rem = 10: - coin=1 → remAmt=9 → dp[10] = 1+dp[9]=4 - coin=2 → remAmt=8 → dp[10] = min(4, 1+dp[8]=4) = 4 - coin=5 → remAmt=5 → dp[10] = min(4, 1+dp[5]=2) = 2 dp = [0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, ∞] rem = 11: - coin=1 → remAmt=10 → dp[11] = 1+dp[10]=3 - coin=2 → remAmt=9 → dp[11] = min(3, 1+dp[9]=4) = 3 - coin=5 → remAmt=6 → dp[11] = min(3, 1+dp[6]=3) = 3 dp = [0, 1, 1, 2, 2, 1, 2, 2, 3, 3, 2, 3] Step 3: End dp[11] = 3 Return 3
Output: 3
Visualisation:
var coinChange = function(coins, amount) {
let n = coins.length;
let dp = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
for (let rem = 1; rem <= amount; rem++) {
for (let j = 0; j < n; j++) {
let remainingAmount = rem - coins[j];
if (remainingAmount >= 0) {
dp[rem] = Math.min(dp[rem], 1 + dp[remainingAmount]);
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount];
};
def coinChange(coins, amount):
n = len(coins)
INF = float('inf')
dp = [INF] * (amount + 1)
dp[0] = 0
for rem in range(1, amount + 1):
for j in range(n):
remainingAmount = rem - coins[j]
if remainingAmount >= 0:
dp[rem] = min(dp[rem], 1 + dp[remainingAmount])
return -1 if dp[amount] == INF else dp[amount]
import java.util.Arrays;
public class Solution {
public int coinChange(int[] coins, int amount) {
int n = coins.length;
final int INF = Integer.MAX_VALUE / 2; // avoid overflow
int[] dp = new int[amount + 1];
Arrays.fill(dp, INF);
dp[0] = 0;
for (int rem = 1; rem <= amount; rem++) {
for (int j = 0; j < n; j++) {
int remainingAmount = rem - coins[j];
if (remainingAmount >= 0) {
dp[rem] = Math.min(dp[rem], 1 + dp[remainingAmount]);
}
}
}
return dp[amount] >= INF ? -1 : dp[amount];
}
}
#include <vector>
#include <algorithm>
#include <limits>
int coinChange(const std::vector& coins, int amount) {
int n = coins.size();
const int INF = std::numeric_limits::max() / 2; // avoid overflow
std::vector dp(amount + 1, INF);
dp[0] = 0;
for (int rem = 1; rem <= amount; ++rem) {
for (int j = 0; j < n; ++j) {
int remainingAmount = rem - coins[j];
if (remainingAmount >= 0) {
dp[rem] = std::min(dp[rem], 1 + dp[remainingAmount]);
}
}
}
return dp[amount] >= INF ? -1 : dp[amount];
}
#include <stdlib.h>
#include <limits.h>
int coinChange(int* coins, int n, int amount) {
int INF = INT_MAX / 2; // avoid overflow when adding 1
int* dp = (int*)malloc((amount + 1) * sizeof(int));
if (!dp) return -1; // allocation failed
for (int i = 0; i <= amount; ++i) dp[i] = INF;
dp[0] = 0;
for (int rem = 1; rem <= amount; ++rem) {
for (int j = 0; j < n; ++j) {
int remainingAmount = rem - coins[j];
if (remainingAmount >= 0) {
int candidate = 1 + dp[remainingAmount];
if (candidate < dp[rem]) dp[rem] = candidate;
}
}
}
int result = (dp[amount] >= INF) ? -1 : dp[amount];
free(dp);
return result;
}
using System;
public class Solution {
public int CoinChange(int[] coins, int amount) {
int n = coins.Length;
int INF = int.MaxValue / 2; // avoid overflow
int[] dp = new int[amount + 1];
for (int i = 0; i <= amount; i++) dp[i] = INF;
dp[0] = 0;
for (int rem = 1; rem <= amount; rem++) {
for (int j = 0; j < n; j++) {
int remainingAmount = rem - coins[j];
if (remainingAmount >= 0) {
dp[rem] = Math.Min(dp[rem], 1 + dp[remainingAmount]);
}
}
}
return dp[amount] >= INF ? -1 : dp[amount];
}
}
