Skip to content

Commit

Permalink
Knapsack.java
Browse files Browse the repository at this point in the history
  • Loading branch information
priyasneha28 authored Oct 1, 2021
1 parent 0acb1ff commit 4b8cf59
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Java/Knapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Arrays;

public class Knapsack {
public static void main(String args[]) {
int w = 10;
int n = 4;
int[] val = {10, 40, 30, 50};
int[] wt = {5, 4, 6, 3};

// Populate base cases
int[][] mat = new int[n + 1][w + 1];
for (int r = 0; r < w + 1; r++) {
mat[0][r] = 0;
}
for (int c = 0; c < n + 1; c++) {
mat[c][0] = 0;
}

// Main logic
for (int item = 1; item <= n; item++) {
for (int capacity = 1; capacity <= w; capacity++) {
int maxValWithoutCurr = mat[item - 1][capacity]; // This is guaranteed to exist
int maxValWithCurr = 0; // We initialize this value to 0

int weightOfCurr = wt[item - 1]; // We use item -1 to account for the extra row at the top
if (capacity >= weightOfCurr) { // We check if the knapsack can fit the current item
maxValWithCurr = val[item - 1]; // If so, maxValWithCurr is at least the value of the current item

int remainingCapacity = capacity - weightOfCurr; // remainingCapacity must be at least 0
maxValWithCurr += mat[item - 1][remainingCapacity]; // Add the maximum value obtainable with the remaining capacity
}

mat[item][capacity] = Math.max(maxValWithoutCurr, maxValWithCurr); // Pick the larger of the two
}
}

System.out.println(mat[n][w]); // Final answer
System.out.println(Arrays.deepToString(mat)); // Visualization of the table
}
}

0 comments on commit 4b8cf59

Please sign in to comment.