BotByte

dp_coins_tc_1.cpp

May 13th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. /* Coin Change - Version II */
  2. /* Author : M. A. Rafsan Mazumder */
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. #define MAX 1000
  9. int arr[MAX], n;
  10. int dp[MAX];
  11.  
  12. int f(int tot)
  13. {
  14.     if(tot == 0) return 0;
  15.     if(dp[tot] != -1) return dp[tot];
  16.     int mins = INT_MAX-1;
  17.     for(int i=0; i<n; i++){
  18.         if(tot - arr[i] >= 0) mins = min(mins, f(tot - arr[i]));
  19.     }
  20.     return dp[tot] = mins + 1;
  21. }
  22.  
  23.  
  24. int main()
  25. {
  26.     memset(dp, -1, sizeof dp);
  27.     scanf("%d", &n);
  28.     for(int i=0; i<n; i++) scanf("%d", &arr[i]);
  29.     int tot;
  30.     scanf("%d", &tot);
  31.     if(f(tot) == INT_MAX) cout << "not possible";
  32.     else cout << f(tot);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment