https://ide.codingblocks.com/s/223389.Please tell me where I am going wrong.
Getting wrong in all test cases
@Hk199977
number of inputs in second line is w, not n. So iterate till i<w line no. 29. Similarly change line 33 to cout<<minimumCost(w,w,price).
I think your dp[i][j] represents minimum cost to achieve j kg using i oranges. So there is mistake in line no. 13, It should be if(i<=j && price[i-1]!=-1). You have not considered the casewhen price[i-1]==-1. Also line 15 should be dp[i][j]=min(dp[i-1][j],dp[i][j-i]+price[i-1]);
At the end, you missed the case when dp[n][m]==INT_MAX; in that case ans doesn’t exist and you have to return -1. So add this case at the end of minimumCost() function.
Your code will work fine after these changes.
@Hk199977
1)move line 23 after line 27
2)line 37 should be cout<<minimumCost(w,w,price); not n
3)line 13 should be if(i<=j && price[i-1]!=-1)
Make these changes
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.