forked from rs992214/Unique_Coder_World
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0acb1ff
commit 4b8cf59
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |