/* This of course assumes that there are no coins which are not integer values,
* and that an array of coins A exists which is length m and is
* indexed A[1..m]. The coin at A[1] = 1 by definition, and A[k] <= A[k + 1].
*/
int coins(int nAmount, int nCoinIndex)
{
if(nAmount == 0) return 0;
else if((nCoinIndex < 1) ||
(nAmount < 0))
{ /* Need to have some representation for INFINITY. It could simply
* be the coint amount + 1.
*/
return INFINITY;
}
else
{
return min(coins(nAmount - A[nCoinIndex], nCoinIndex) + 1,
coins(nAmount, nCoinIndex - 1));
}
}
Consider T(n). Oh my goodness! We have double recursion going on here. We have a stop state for nAmount, as well as one for nCoinIndex.
Aren't you glad I am not giving this for the midterm! :-)
Minimize constants, the if decision statement and the end of the recursion can be time c1. The else will be the tricky part.
Here we see a call to min that has two recursive calls. Hence, both calls will be made to determine which is the minimum.