# Solution For Coin Change

The Coin Change problem on LeetCode is a classic dynamic programming problem where we are given a set of coins and a target amount to reach with those coins. The problem statement is as follows:

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.

For example, given coins = [1, 2, 5] and amount = 11, the function should return 3 (11 = 5 + 5 + 1).

One way to approach this problem is to use dynamic programming. We can create an array dp where dp[i] represents the minimum number of coins needed to make up the amount i.

We can start by initializing dp to 0, since it takes zero coins to make up an amount of zero. Then for each amount i from 1 to the target amount, we can iterate over all the coins less than or equal to i and consider two possibilities:

1. We can use the coin to make up the amount i. In this case, we need to find the minimum number of coins needed to make up the remaining amount (i – coin). Since we are looking for the minimum number of coins, we can take the minimum of dp[i] and dp[i – coin] + 1.
2. We cannot use the coin to make up the amount i. In this case, we do not need to do anything as dp[i] remains the same.

We can continue this process for all amounts from 1 to the target amount, and the final result will be stored in dp[amount]. If there is no combination of coins that can make up the amount, then dp[amount] will remain at its initial value of infinity, and we can return -1 in this case.

Here’s the implementation of the above algorithm in Python:

```def coinChange(coins, amount): # Initialize dp array with infinity dp = [float('inf') for _ in range(amount + 1)] # Base case: zero coins are needed to make amount 0 dp = 0 # Iterate over all amounts from 1 to target amount for i in range(1, amount + 1): # Iterate over all coins less than or equal to i for coin in coins: # Check if coin can be used to make up amount i if coin <= i: # Take minimum of using coin and not using coin dp[i] = min(dp[i], dp[i - coin] + 1) # If amount cannot be made up by any combination of coins, return -1 if dp[amount] == float('inf'): return -1 else: return dp[amount]```

The time complexity of the above algorithm is O(amount * n), where n is the number of coins, since we are iterating over all amounts and all coins less than or equal to each amount. The space complexity is O(amount) since we are using dp array of size amount + 1.

Overall, this algorithm provides an efficient solution to the Coin Change problem with the time complexity dependent only on the size of the input.

## Step by Step Implementation For Coin Change

```You can use a greedy algorithm to solve this problem. The idea is to always choose the largest coin that you can use to make the change.

public int coinChange(int[] coins, int amount) {

if (amount == 0) {

return 0;

}

int numCoins = 0;

int i = coins.length - 1;

while (amount > 0 && i >= 0) {

if (amount >= coins[i]) {

amount -= coins[i];

numCoins++;

} else {

i--;

}

}

if (amount == 0) {

return numCoins;

} else {

return -1;

}

}```
```def coinChange(self, coins: List[int], amount: int) -> int:

#Create an array to store the minimum number of coins needed to make change for each value from 1 to the amount
min_coins = [float("inf")] * (amount + 1)
min_coins = 0

#Loop through each coin value
for coin in coins:

#Loop through each value from 1 to the amount
for i in range(coin, amount + 1):

#If the coin value is less than the current value, update the minimum number of coins needed for that value
if coin <= i:
min_coins[i] = min(min_coins[i], min_coins[i - coin] + 1)

#If the minimum number of coins needed to make change for the amount is still infinity, return -1. Otherwise, return the minimum number of coins.
return -1 if min_coins[amount] == float("inf") else min_coins[amount]```
```You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

// coins is an array of integers representing the different denominations of coins
// amount is an integer representing the total amount of money

var coinChange = function(coins, amount) {
// create a 2D array of size (amount + 1) x (coins.length + 1) to hold the solutions to subproblems
// initialize the first row and column to 0
var dp = new Array(amount + 1);
for (var i = 0; i <= amount; i++) {
dp[i] = new Array(coins.length + 1).fill(0);
}
for (var i = 0; i <= coins.length; i++) {
dp[i] = 1;
}

// fill in the rest of the table according to the following recursive relation:
// dp[i][j] = dp[i][j - 1] (if the coin at index j is too large for the amount i)
//          + dp[i - coins[j]][j] (if the coin at index j can be used)
for (var i = 1; i <= amount; i++) {
for (var j = 1; j <= coins.length; j++) {
if (coins[j - 1] > i) {
dp[i][j] = dp[i][j - 1];
} else {
dp[i][j] = dp[i][j - 1] + dp[i - coins[j - 1]][j];
}
}
}

// the number of combinations that make up the amount is stored in the bottom right corner of the table
return dp[amount][coins.length];
};```
```You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.

class Solution {
public:
int change(int amount, vector& coins) {
// this vector keeps track of the number of ways to make change for
// all amounts from 0 to amount
vector dp(amount + 1);

// there is only one way to make change for 0 - using 0 coins
dp = 1;

// for each coin, we update the dp vector to reflect the new number
// of ways to make change for all amounts from 0 to amount
for (int coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}

// at the end, dp[amount] will hold the number of ways to make change
// for the given amount using the given coins
return dp[amount];
}
};```
```using System;

public class GFG {

// Returns the count of ways we can
// sum S[0...m-1] coins to get sum n
public static int count( int S[], int m, int n )
{
// If n is 0 then there is 1
// solution (do not include any coin)
if (n == 0)
return 1;

// If n is less than 0 then no
// solution exists
if (n < 0)
return 0;

// If there are no coins and n
// is greater than 0, then no
// solution exist
if (m <=0 && n >= 1)
return 0;

// count is sum of solutions (i)
// including S[m-1] (ii) excluding S[m-1]
return count( S, m - 1, n ) + count( S, m, n-S[m-1] );
}

// Driver program
public static void Main()
{
int i, j;
int arr[] = {1, 2, 3};
int m = arr.Length;
Console.Write("Enter the amount :");

// Print table for all values from 1 to amount
for (i = 0; i <= amount; i++)
{
for (j = 0; j < m; j++)
if (i-arr[j] >= 0)
Console.Write(count(arr, j, i-arr[j]) + " ");
else
Console.Write("0 ");
Console.WriteLine();
}
}
}```

## Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]