-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsack.java
More file actions
31 lines (29 loc) · 811 Bytes
/
Knapsack.java
File metadata and controls
31 lines (29 loc) · 811 Bytes
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
import java.util.*;
class Knapsack{
public static void main(String args[]){
int wts[] = {12,15,17,25};
int profit[] = {25,67,45,72};
int cap = 40;
knapsack(wts,profit,cap);
}
static void knapsack(int wts[],int profit[],int cap){
int i, w;
int n = profit.length;
int table[][] = new int[n+1][cap+1];
for(i=0;i<=n;i++){
table[i][0] = 0;
}
for(i=0;i<=cap;i++){
table[0][i] = 0;
}
for (i = 1; i <= n; i++){
for (w = 1; w <= cap; w++){
if (wts[i-1] <= w)
table[i][w] = Integer.max(profit[i-1] + table[i-1][w-wts[i-1]], table[i-1][w]);
else
table[i][w] = table[i-1][w];
}
}
System.out.println(table[n][cap]);
}
}