-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack01.java
More file actions
60 lines (48 loc) · 1.35 KB
/
knapsack01.java
File metadata and controls
60 lines (48 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.io.*;
import java.util.Arrays;
public class knapsack01 {
static int dp[][] = new int[5][8];
static int knapsack(int wt[], int val[], int w, int n){
if(n==0||w==0){
return 0;
}
if(dp[n][w]!=-1){
return dp[n][w];
}
if(wt[n-1] > w){
return dp[n][w] = knapsack(wt, val, w, n-1);
}
return dp[n][w] = Math.max(val[n-1] + knapsack(wt, val, w - wt[n-1], n-1), knapsack(wt, val, w, n-1));
}
public static void main(String[] args){
for(int[] row:dp)
Arrays.fill(row, -1);
int wt[]={1,3,4,5};
int val[]={1,4,5,7};
int w=7,n=4;
// knapsack(wt, val, w, n);
for(int i =0;i<n+1;i++){
for(int j = 0;j<w+1;j++){
if(i==0||j==0){
dp[i][j]=0;
}
}
}
for(int i=1;i<5;i++){
for(int j=1;j<8;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];
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<8;j++){
System.out.print(dp[i][j]+" ");
}
System.out.println();
}
}
}