Skip to content

Instantly share code, notes, and snippets.

@Sharma96
Created July 22, 2017 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sharma96/9796140c1b12f39aad2f216a3bb889bc to your computer and use it in GitHub Desktop.
Save Sharma96/9796140c1b12f39aad2f216a3bb889bc to your computer and use it in GitHub Desktop.
//consider dp[] to be a global array
int minCoins(int N, int M)
{
if(dp[N] != -1)
return dp[N];
if (N == 0)
return 0;
int res = INF;
for(int i=0;i<M;i++){
if(coins[i]<=N){
int sub_res = 1+minCoins(N-coins[i],M);
if (sub_res < res)
res = sub_res;
}
}
return dp[N] = res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment