-
Notifications
You must be signed in to change notification settings - Fork 20k
#4367 Enhance Knapsack problem #4368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two other solutions of that problem: DyanamicProgrammingKnapsack and KnapsackMemoization.java - what is the relation between them and the one you update?
Besides that I suggest some cosmetic changes.
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java
Outdated
Show resolved
Hide resolved
Seems like this is DyanamicProgrammingKnapsack duplicate of what I just refactored, and KnapsackMemoization.java is recursive solition of this problem. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good👍
@siriak how should we proceed? |
Please remove the duplicate, but leave the version with memoization as it's a different approach |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to use meaningful test names instead of comments. Names will appear in the output in case if they fail, Comments will not.
Thanks for adding missing tests!
Besides that: the code looks good.
src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java
Outdated
Show resolved
Hide resolved
Head branch was pushed to by a user without write access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if one of the entries of weights
is negative or 0
?
The following code:
Knapsack.knapSack(1, new int[]{0, 0, 0, 0}, new int[]{1, 1, 1, 1})
gives me 16
and I think the correct answer should be 4
.
Knapsack problem have positive weights normally, should we add extra check for non positive weights ? |
Yes! Throwing an error is the best way of saying that non-positive weights are forbidden. |
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
src/main/java/com/thealgorithms/dynamicprogramming/Knapsack.java
Outdated
Show resolved
Hide resolved
Sorry for the previous mess.
Reduced space complexity of solution from O(n*w) to O(w)
Named variables better
Added testfile
All code is covered in testcases
