Min coin change

#include <bits/stdc++.h>
using namespace std;
int mincoin(int n,int coin[],int t){
int dp[100]={0};
for(int i=1;i<=n;i++){
dp[i]=INT_MAX;
for(int j=0;j<t;j++){
if(n-coin[j]>=0){
int ans=dp[n-coin[j]];
dp[i]=min(dp[i],ans+1);
}
}
}
return dp[n];
}
int main() {
int n=15;
int coin[]={1,7,10};
int t= sizeof(coin)/sizeof(int);
int ans=mincoin(n,coin,t);
cout << ans;
}

output is not correct

@soniyaman931
inside your second for loop you should check if(i-coin[j]>=0) and update ans with dp[i-coin[j]] you have taken n while your iterator is i.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

1 Like