The Problem
Greedy is a coin change making problem, in which we are supposed to tell the minimum number of coins that add to make a certain value. The available coins are quarters(25), dimes(10), nickels(5), and pennies(1). For example, if a change of 30 is to be made, then
30 = 25 + 5 // this is what we need as minimum number of coins are required for the transaction
30 = 10 +10 + 10 // we don't bother of this
30 = 10 + 10 + 5 + 5 // and neither this, because the number of coins returned are greater than 2(which we got in the first case)
... and other combinations.
Let n
be the amount of change to be given and count
be the number of coins that are to be returned.(Remember that we need to minimize the number of coins i.e. count
). Let W = {25,10,5,1}, w(i) ∈ W, 1 <= i <= 4.
Brute Force
A simple solution that strikes to our mind is to take the value(n) and then check if it is greater than or equal to the w(i), if it is, then increase count
by 1 and reduce n
by w(i). Here is the python script for it.(I preferred writing .py over pseudocode)
n = 40 # change to be provided
count = 0
while n > 0 :
if n >= 25 :
n = n - 25
count = count + 1
elif n >= 10 :
n = n - 10
count = count + 1
elif n >= 5 :
n = n - 5
count = count + 1
elif n >= 1 :
n = n - 1
count = count + 1
print (count)
http://ideone.com/2qjaCw
As beginner this was a good approach, but our algorithm has a loop that runs in O(n), so when the value of n
is sufficiently large, then our program becomes slightly slow.(Although O(n) is not considered that poor, actually it is a good one.)
Heuristic Approach
But still there is a better way to do this, almost assumed to be in constant time.
n = 40 # change to be provided
count = 0
W = [25, 10, 5, 1]
temp = n
for i in range(0,4):
t = (int)(temp/W[i])
count = count + t
temp = temp - t*W[i]
print (count)
http://ideone.com/ged3rS
In this approach, we keep the possible values of w(i) in an array W which is already stored. Then we iterate only 4 times(independent of what is the amount of change to be provided) and therefore we perform better even when there is a large input(the value of change).
At each iteration, we check for the amount of coins returned for a particular value of w(i). In the first iteration, we calculate the maximum number of coins worth 25 required to owe the change. And that's easily predictable that for any value n
, the number of coins required will be the multiple of W[i]
, which is just less than n
, divided by W[i]
itself. For example, let us take n = 1024
, then,
_________________________________________________________________________________
|Iteration | Value of coin under process | Value of n | Number of coins required |
|_________________________________________________________________________________|
| | | | |
| 1 | 25 | 1024 | (int(1024/25)) = 40 |
| 2 | 10 |1024-25*40=24| (int(24/10)) = 2 |
| 3 | 05 | 24-10*2=4 | (int(4/5)) = 0 |
| 4 | 01 | 4-5*0=4 | (int(4/1)) = 4 |
_________________________________________________________________________________
In this way, the total value of count
becomes 40 + 2 + 0 + 4 = 46
. We store variables in W
in descending order because we need to minimize the count
, if we stored the values in W
in ascending order, then we would have been maximizing the value of count
.