-
Notifications
You must be signed in to change notification settings - Fork 21
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
Showing
1 changed file
with
71 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,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]; | ||
} | ||
} | ||
``` |