Advertisement
Misbah_Uddin_Tareq

Coin Change (dp)

Feb 6th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. #define pb push_back
  5. const ll mx=1005;
  6.  
  7. int coin[]= {50,25,10,5,1};
  8. int make,dp[mx][mx];
  9.  
  10. int coin_change(int i, int amount)
  11. {
  12.     if(i>=5)
  13.     {
  14.         if(amount==0)
  15.             return 1;
  16.         else
  17.             return 0;
  18.     }
  19.  
  20.     if(dp[i][amount]!=-1)
  21.         return dp[i][amount];
  22.     int ret1=0,ret2=0;
  23.  
  24.     if(amount-coin[i]>=0)
  25.         ret1=coin_change(i,amount-coin[i]);
  26.     ret2=coin_change(i+1,amount);
  27.  
  28.     return dp[i][amount]=ret1+ret2;
  29.    
  30.     /// for finding how many way ret1`+ret2;
  31.     /// for say yes or no ret1 | ret2;
  32. }
  33.  
  34. int main()
  35. {
  36.     memset(dp,-1,sizeof dp);
  37.     while(cin>>make)
  38.     {
  39.         cout<<coin_change(0,make)<<endl;
  40.     }
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement