Skip to content

Commit

Permalink
up 27/02/22
Browse files Browse the repository at this point in the history
  • Loading branch information
RajSDE committed Feb 27, 2022
1 parent 146e1b1 commit 40e738f
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions JavaPractice/0-1_KnapsackProblem/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
## 0 - 1 Knapsack Problem
You are given weights and values of N items, put these items in a knapsack of capacity W to get the maximum total value in the knapsack. Note that we have only one quantity of each item.
In other words, given two integer arrays val[0..N-1] and wt[0..N-1] which represent values and weights associated with N items respectively. Also given an integer W which represents knapsack capacity, find out the maximum value subset of val[] such that sum of the weights of this subset is smaller than or equal to W. You cannot break an item, either pick the complete item or dont pick it (0-1 property). [🔗Goto](https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1/?page=2&difficulty[]=1&status[]=unsolved&company[]=Amazon&sortBy=submissions#)

<details>
<summary>Full Code</summary>

```java
import java.util.*;
import java.io.*;
import java.lang.*;

class gfg
{
public static void main(String args[])throws IOException
{
//reading input using BufferedReader class
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

//reading total testcases
int t = Integer.parseInt(read.readLine());

while(t-- > 0)
{
//reading number of elements and weight
int n = Integer.parseInt(read.readLine());
int w = Integer.parseInt(read.readLine());

int val[] = new int[n];
int wt[] = new int[n];

String st[] = read.readLine().trim().split("\\s+");

//inserting the values
for(int i = 0; i < n; i++)
val[i] = Integer.parseInt(st[i]);

String s[] = read.readLine().trim().split("\\s+");

//inserting the weigths
for(int i = 0; i < n; i++)
wt[i] = Integer.parseInt(s[i]);

//calling method knapSack() of class Knapsack
System.out.println(new Solution().knapSack(w, wt, val, n));
}
}
}
```
</details>

```java
class Solution
{
static int knapSack(int W, int wt[], int val[], int n)
{
int[][] dp = new int[n+1][W+1];
for(int i=1; i<=n; i++){
for(int j=1; j<=W; j++){
if(wt[i-1] <= j){
dp[i][j] = Math.max(val[i-1] + dp[i-1][j-wt[i-1]], dp[i-1][j]);
}
else{
dp[i][j] = dp[i-1][j];
}
}
}
return dp[n][W];
}
}
```

0 comments on commit 40e738f

Please sign in to comment.