Please find mistake in my code. It seems correct to me.
Incorrect Answer Minimum Money
Hi @rishabhsetiya7,
see your code resembles a lot to what is given on the web apart from a few thing that you changed… But in changing those thing it seems you have made conceptual error . For example it is given in the question that -1 means that the weight does not exist for sale … Hence you did not take care of that … There are a lot of logical errors … Please correct them
You are right in saying that. But in the sample input, there is no case of -1 and still it is not working. I also know there must be some mistakes due to which the code is not giving correct answer. I hope you will be able to point them out concretely.
check the comment in the following code
I have added a condition for excluding items not available. Please see what is wrong with it
I will reply to you asap …
Hey, I have checked your solution . First of all it is very much copied and i will have to report that and secondly you have not used any logic for solving the problem and just copied what was given on geeksforgeeks… I have tried the test cases on geeksforgeeks solution too it gives wrong answer and so does your solution . That means the geeks algorithm is not for this question . As i told you before ,stop copying and use your mind to think for a solution . Even if you copy exact solution of geeks for geeks that won’t work either.
Apart from that your solution is correct but wont work for this problem .
I copied Knapsack code from Prateek sir’s webinar video and modified that according to the hint video given in the course.
And even if you think, I have copied from Geeks for Geeks, please let me know the fault, so that I can mail them the correction to be done, or you do it yourself, so that people don’t read an incorrect solution.
See when you form a matrix of dimension it compares with prices that you have given . And as per the your code when it encounters -1 for the row ‘i’ then for row ‘i’ it gives correct answer but for row i+1 it has to compare the minimum in row i and row i-1 so in that case the -1 is again choosen as the minimum is -1 . Therefore the answer comes out to be -1 . For the cases in which there is no negative value , the process of comparing is wrong . If we dry run the code then we will see that for some values it actually takes up 0 as the value and hence gives the particular cell a wrong value … Which cell is affected is determined by the test case .
However a simpler approach will be benifitial where the code compares between the minimum if a weight is taken or a weight is not taken . A dynamic code can be formed for the following recursion code…
I am trying a test case 5 5 20 2 -1 10 50 which is giving answer 24 in your code as well as my code. Could you please provide some cases where my code gives answer different from your code, so that I can compare dry run of both.
5 10
-1 33 -1 -1 -1 -1 -1 120 -1 300
answer is =153
5 5
-1 4 3 2 1
answer = 1