Advertisement
Fastrail08

Coin Change Permutations

May 20th, 2025
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. //QUESTION - https://www.youtube.com/watch?v=yc0LunmJA1A&list=PL-Jc9J83PIiG8fE6rj9F5a6uyQ5WPdqKy&index=14 (COIN CHANGE PERMUTATIONS)
  6.  
  7.  
  8. //To memoise the code, figure out the param that is changing at every state and can be used to UNIQUELY identify all the states, so that the answer could be stored against it.
  9.  
  10. // Here amount is the param that can be memoised, as it can UNIQUELY identify all the states.
  11. //How many ways to pay up given amount, when you currently have x amount.
  12.  
  13. // Recursive code + memo
  14. int getAllCoinChangePerm(int level, int &amount, vector<int> &coins, string currPerm, vector<int> &memo){
  15.     if(level > amount){
  16.         return 0;
  17.     }
  18.     if(level == amount){
  19.         // You can't print if you memoise the code, because printing happens at base case, and memoised code avoid going to base case each time, if you have answer already
  20.         // cout << currPerm << '\n';
  21.         return 1;
  22.     }
  23.     if(memo[level] != 0){
  24.         return memo[level];
  25.     }
  26.     // level = amount made
  27.     //options = try each currency to form all the possible permutations to reach amount
  28.     int totalWays = 0;
  29.     for(int i = 0; i < coins.size(); i++){
  30.         totalWays += getAllCoinChangePerm(level + coins[i], amount, coins, currPerm + to_string(coins[i]), memo);
  31.     }
  32.     return memo[level] = totalWays;
  33. }
  34.  
  35. int getAllCoinChangePermDP(int &amount, vector<int> &coins){
  36.     // Storage & Meaning - dp[i] = how many ways to pay 'amount' if you already have dp[i] amount
  37.     vector<int> dp(amount + 1, 0);
  38.    
  39.     //Direction - figure out where the smallest problem lie(base case in recursion), initialise it, & build up to larger problems
  40.     //smallest problem = dp[amount] = 1, you have 1 way to pay amount, if you are already dp[amount], pay nothing so 1 way
  41.     dp[amount] = 1;
  42.    
  43.     //Travel & Solve - Travel towards to larger problem, build answer for larger problems, by analysing the recursion tree, by checking what calls are made on the way up (smaller problem -> larger problems)
  44.     //NOTE-IMPORTANT
  45.     //IN recursion we went down from larger problems to smaller problems, just see the tree, and figure out what's the relationship between the parent and child states in TERMS OF MEMOISED PARAMS. Exactly those calls will be made in dp array.
  46.     //For Eg - nth fib calls to 2 children states (n - 1) and (n - 2) while going down the recursive tree
  47.     //DP solves on the way up on recursion tree, you will have answers to all the smaller problems. Now you just need to figure out what smaller problems to call to get the values for the current state and that would exactly be the calls in recursion tree (parent -> child in recursion, child-> parent in DP)
  48.     // In DP, the answer for nth fib will be a call to (n - 1) & (n - 2)
  49.     //Alternatively you could also think as..
  50.     //dp[n] will be responsible for BUILDING the answer for dp[n + 1] & dp[n + 2] (IMPLEMENT THIS)
  51.    
  52.     for(int i = amount - 1; i >= 0; i--){
  53.         for(int j = 0; j < coins.size(); j++){
  54.             if(i + coins[j] <= amount){
  55.                 dp[i] += dp[i + coins[j]];
  56.             }
  57.         }
  58.     }
  59.     return dp[0];
  60. }
  61.  
  62. int main() {
  63.     // your code goes here
  64.     int n, amt;
  65.     cin >> n;
  66.     vector<int> coins(n);
  67.     for(int i = 0; i < n; i++){
  68.         cin >> coins[i];
  69.     }
  70.     cin >> amt;
  71.     vector<int> memo(amt + 1, 0);
  72.     cout << getAllCoinChangePerm(0, amt, coins, "", memo) << '\n';
  73.     cout << getAllCoinChangePermDP(amt, coins) << '\n';
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement