-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapsackSolver.java
More file actions
43 lines (31 loc) · 1.06 KB
/
Copy pathKnapsackSolver.java
File metadata and controls
43 lines (31 loc) · 1.06 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
import java.util.*;
import java.lang.*;
import java.io.*;
class KnapsackSolver {
public static int knapSack(int W, int wt[], int vals[], int n){
int myTable[][] = new int [n][W]; //creates a 2D array
for(int i=1; i<=n; i++ ){
for(int j=1; j<=W; j++ ){
int option1 = vals[i]+myTable[i-1][j-wt[i]];
int option2 = myTable[i-1][j];
if(W==0 | n==0){ //either the sack is empty or there is no item to take
myTable[i][j] = 0;
}
if(wt[i]<=j){
myTable[i][j]= Math.max(option1, option2);
}else{
myTable[i][j]= option2;
}
}
}
return myTable[n][W];
}
public static void main (String[] args) {
//testing the meothod above
int val[] = new int[]{60, 100, 120};
int wt[] = new int[]{10, 20, 30};
int W = 50;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}