This is another interesting problem that can be solved using dynamic programming. In this problem we have coins of different denominations {C1, C2, C3, C4.……Cn} and an amount S, then in how many ways we can make change for S amount using n coins. For example if we have coins of denominations 1, 2, 5 then for making change for amount 6 various ways are {1,1,1,1,1,1}, {1,1,1,1,2}, {2,2,2}, {1,1,2,2}, {1,5}. So there are 4 ways to make change for amount 6 using {1,2,5} coins.
Lets say that W(i, s) be total number of ways to make change for s amount using i coins. Then W(i, s) can be expressed as:
W(i, s) = W(i-1, s) + W(i, s-c[i])
W(i-1, s) --> total number of ways to make change for s amount without using ith coin.
W(i, s-c[i]) --> all ways that include atleast one ith coin
So total number of ways is sum of 2 different sub problems thus it has optimal substructure property.
There are few boundary conditions for above function:
1. W(i, s) = 1, if i >=0 and s = 0.
if we have no amount to make change but we have coins then there is only one way which is using no coins at all.
2. W(i, s) = 0, if i = 0 and s > 0
i.e. there is an amount for making change but we have no coins to make change then there is 0 way to make change.
3. W(i, s) = W(i-1, s), if s < c[i]
if amount s is less than value of coin than number of ways is equal to ways in which ith coin is not included.
4. W(i, s) = W(i-1, s) + W(i, s-c[i]), if i>=1 and s>=c[i]
if number of coins greater than equal to 1 and amount s greater than equal to value of coins then number of ways is sum of ways without ith coin and ways that include atleast one occurence of ith coin.
To solve this problem we can call above function recursively taking care of base cases discuss above. But in that case there are lot of repetitive recursive calls. So this problem also has overlapping sub-problems property.
Below is recursive approach without using dynamic programming.
We can improve performance by using bottom up dynamic programming using tabulation instead of using recursion. Below is bottom up dynamic programming solution to problem.
In above solution we are storing number of ways for each coin in 2-D array. We can optimize to use only 1-D array since every time loop runs 1-D array contain number of ways till previous coin.Memory optimize version of above solutions is:
Lets say that W(i, s) be total number of ways to make change for s amount using i coins. Then W(i, s) can be expressed as:
W(i, s) = W(i-1, s) + W(i, s-c[i])
W(i-1, s) --> total number of ways to make change for s amount without using ith coin.
W(i, s-c[i]) --> all ways that include atleast one ith coin
So total number of ways is sum of 2 different sub problems thus it has optimal substructure property.
There are few boundary conditions for above function:
1. W(i, s) = 1, if i >=0 and s = 0.
if we have no amount to make change but we have coins then there is only one way which is using no coins at all.
2. W(i, s) = 0, if i = 0 and s > 0
i.e. there is an amount for making change but we have no coins to make change then there is 0 way to make change.
3. W(i, s) = W(i-1, s), if s < c[i]
if amount s is less than value of coin than number of ways is equal to ways in which ith coin is not included.
4. W(i, s) = W(i-1, s) + W(i, s-c[i]), if i>=1 and s>=c[i]
if number of coins greater than equal to 1 and amount s greater than equal to value of coins then number of ways is sum of ways without ith coin and ways that include atleast one occurence of ith coin.
To solve this problem we can call above function recursively taking care of base cases discuss above. But in that case there are lot of repetitive recursive calls. So this problem also has overlapping sub-problems property.
Below is recursive approach without using dynamic programming.
public class CoinChange {
|
Java2html |
Number of ways:289444
We can improve performance by using bottom up dynamic programming using tabulation instead of using recursion. Below is bottom up dynamic programming solution to problem.
public class CoinChange {
|
Java2html |
Number of ways:289444
In above solution we are storing number of ways for each coin in 2-D array. We can optimize to use only 1-D array since every time loop runs 1-D array contain number of ways till previous coin.Memory optimize version of above solutions is:
static int numOfWays1(int [] coins, int amount){
|
Java2html |