https://ide.codingblocks.com/s/662526 one test case showing error amount=3 and coins[]=[2];
Link of problem. https://leetcode.com/problems/coin-change/
Leetcode One tet case showing error
Hey @mohit26900
Try this–>
class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int dp[amount+1];
dp[0] = 0;
for(int i=1;i<=amount;i++){
dp[i] = INT_MAX;
for(int j=0;j<coins.size();j++){
if(i-coins[j] >=0 && dp[i-coins[j]] != INT_MAX){
dp[i] = min(dp[i], dp[i-coins[j]]+1);
}
}
}
if(dp[amount] == INT_MAX){
return -1;
}
return dp[amount];
}
};
This is Bottom Up approach please help me in Top down Whats wrong in my code
int dp[10000 + 1][12 + 1];
int memoization(vector<int>& wt, int w, int n)
{
if (n == 0 || w == 0)
return (w == 0) ? 0 : INT_MAX - 1;
if (dp[w][n] != -1)
return dp[w][n];
if (wt[n - 1] > w)
return dp[w][n] = 0 + memoization(wt, w - 0, n - 1);
else
return dp[w][n] = min(0 + memoization(wt, w - 0, n - 1), 1 + memoization(wt, w - wt[n - 1], n));
}
int coinChange(vector<int>& coins, int amount)
{
memset(dp, -1, sizeof(dp)); // New Line Added
int minCoins = memoization(coins, amount, coins.size());
return minCoins == INT_MAX - 1 ? -1 : minCoins;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.