Skip to content

Commit 229d28b

Browse files
committed
Feat:add 0/1knapsack and 0/1knapsacktabulation along with their tests
1 parent 40b8bc5 commit 229d28b

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTabulation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,19 @@ private KnapsackZeroOneTabulation() {
2626

2727
/**
2828
* Solves the 0-1 Knapsack problem using the bottom-up tabulation technique.
29-
*
3029
* @param values the values of the items
3130
* @param weights the weights of the items
3231
* @param capacity the total capacity of the knapsack
3332
* @param itemCount the number of items
3433
* @return the maximum value that can be put in the knapsack
35-
* @throws IllegalArgumentException if input arrays are null, of different lengths,
36-
* or if capacity or itemCount is invalid
34+
* @throws IllegalArgumentException if input arrays are null, of different lengths,or if capacity or itemCount is invalid
3735
*/
3836
public static int compute(final int[] values, final int[] weights, final int capacity, final int itemCount) {
3937
if (values == null || weights == null) {
4038
throw new IllegalArgumentException("Values and weights arrays must not be null.");
4139
}
4240
if (values.length != weights.length) {
43-
throw new IllegalArgumentException("Values and weights arrays must be non-null and of same length");
41+
throw new IllegalArgumentException("Values and weights arrays must be non-null and of same length.");
4442
}
4543
if (capacity < 0) {
4644
throw new IllegalArgumentException("Capacity must not be negative.");

src/test/java/com/thealgorithms/dynamicprogramming/KnapsackZeroOneTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.dynamicprogramming;
22

3-
import static org.junit.jupiter.api.Assertions.*;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

0 commit comments

Comments
 (0)