Apply the dynamic programming algorithm to find all the solutions to the change-making problem for the denominations 1, 3, 5 and the amount n = 9.
To apply the dynamic programming algorithm to solve the change-making problem for the denominations 1, 3, 5 and the amount n = 9 using a bottom-up approach.
Here are the steps as follows:
Create an array dp of size (n+1) and initialize all elements to infinity except dp[0] = 0, where n is the desired amount. This array will store the minimum number of coins needed to make change for each amount from 0 to n.
For each coin denomination, iterate from the smallest denomination to the largest denomination:
The value in dp[n] will represent the minimum number of coins needed to make change for the amount n.
To find the actual coins used, we can backtrack through the dp array:
Initialize a variable amount = n.
Initialize an empty array result[] to store the coins used.
While amount > 0:
The result[] array will contain the coins used to make change for the amount n.
Let's apply these steps to find all the solutions to the change-making problem for the denominations 1, 3, 5 and the amount n = 9:
Create the dp array: dp = [∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞] dp[0] = 0
Iterate through each denomination:
For denomination = 1:
For denomination = 3:
For denomination = 5:
The value in dp[9] = 1 represents the minimum number of coins needed to make change for the amount 9.
To find the coins used, backtrack through the dp array:
Set amount = 9.
Initialize an empty array result[].
While amount > 0:
The result[] array will contain the coins used to make change for the amount 9. (In this case, result[] = [5, 1, 1, 1, 1])
Hence, for the denominations 1, 3, 5 and the amount n = 9, the minimum number of coins required to make change is 1, and the coins used are [5, 1, 1, 1, 1].