Advertisement
Singasking

Untitled

Jan 19th, 2023
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int f(int index,int target,vector<int>& coins,vector<vector<int>> dp) {
  4.     if(target==0) return 1;
  5.     if(index==0 ) {
  6.         if(target%coins[0]==0) return 1;//target/coins[0];
  7.         return 0;
  8.     }
  9.     if(dp[index][target]!=-1) return dp[index][target];
  10.     int pick = (target-coins[index]>=0)?f(index,target-coins[index],coins,dp):0;
  11.     int npick = f(index-1,target,coins,dp);
  12.     return  dp[index][target]=pick+npick;
  13. }
  14.     int change(int amount, vector<int>& coins) {
  15.         vector<vector<int>>dp(coins.size()+1,vector<int>(amount+1,-1));
  16.         return f(coins.size()-1,amount,coins,dp);
  17.     }
  18. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement