title | date | lastmod |
---|---|---|
Making Change |
2022-11-08 |
2022-11-21 |
[!NOTE] Note that The minimum number of coins to form value x, is 1 + the minimum of the solutions to value
$x-d_1, x-d_2....x-d_k$
We can form the following recurrence equations:
Base case (when n-d less than 0):
int coinchange (int n) {
int change[n+1]; // initialize dp array
for (int j=0; j<=n; j++) {
change[j] = MAXINT // MAXINT is the maximum integer value
for(int i=0;i<m;i++){
if(j-val[i]>=0) {
change[j] = min(change[j],change[j-val[i]]+1)
}
}
if(change[j]== MAXINT) {
change[j]=0; // If no changes means base case 0
}
}
return change[n]
}