Quantcast
Channel: Stories applauded for by Malay Nandasana on Medium
Viewing all articles
Browse latest Browse all 10

Change-making problem : Dynamic method

$
0
0

Change-making problem : Dynamic method

The change-making problem addresses the question of finding the minimum number of coins (of certain denominations) that add up to a given amount of money. It is a special case of the integer knapsack problem, and has applications wider than just currency.

It is also the most common variation of the coin change problem, a general case of partition in which, given the available denominations of an infinite set of coins, the objective is to find out the number of possible ways of making a change for a specific amount of money, without considering the order of the coins.

making change is minimization problem, we choose the solution where we have to give minimum no of coins for given amount.

Methods of solving

  1. Greedy Method

2. Dynamic Method

dynamic programming

A classic dynamic programming strategy works upward by finding the combinations of all smaller values that would sum to the current threshold.Thus, at each threshold, all previous thresholds are potentially considered to work upward to the goal amount W. For this reason, this dynamic programming approach may require a number of steps that is at least quadratic in the goal amount W.

optimal coin change We have seen this, you want to make change using a system of denominations, using the smallest number of coins possible. Sometimes the greedy algorithm gives the optimal solution. But sometimes it does not — an example was the system (12, 5, 1), where the dynamic algorithm gives 15 = 12 + 1 + 1 + 1 but a better answer is 15 = 5 + 5 + 5. but dynamic always gives optimal solution or possible minimum coins within that denominators.

Steps :-

We will find the minimum no. of coins required for the amount .
Amount is the amount for which we are making the change.

we will first set min. no. of coins required i.e., min = INFINITY
This means we initially don’t know how many coins will be needed.

Then we check each denomination coins and see if it can be used to get the solution.

If its possible then we update the min and coin variables.
where, min = minimum no. of coins required for making change for amount
coin = first index of the coin in the solution

Example:

Amount = 5

coins [] = {1,2,3}

Ways to make change = 5

{1,1,1,1,1} {1,1,1,2}, {1,2,2}, {1,1,3} {2,3}

formula to solve coin change using dynamic method:

where, d[i]=denominator , C[p] is minimum no coin. and selected denomination is not greater than the amount p.


Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>