forked from hrsvrdhn/DP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnapSack01.java
More file actions
36 lines (33 loc) · 815 Bytes
/
KnapSack01.java
File metadata and controls
36 lines (33 loc) · 815 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
32
33
34
35
36
import java.util.*;
import java.io.*;
class KnapSack01 {
public static void main(String args[]) {
int[][] items = {
{1, 1},
{3, 4},
{4, 5},
{5, 7}
};
int max_weight = 7;
int[][] dp = new int[items.length+1][max_weight+1];
for(int i=0; i<items.length; i++)
dp[i][0] = 0;
for(int i=0; i<=max_weight; i++)
dp[0][i] = 0;
for(int i=1; i<=items.length; i++) {
for(int j=1; j<=max_weight; j++) {
int c = items[i-1][0], w = items[i-1][1];
if(j < c)
dp[i][j] = dp[i-1][j];
else
dp[i][j] = Math.max(dp[i-1][j], w + dp[i-1][j-c]);
}
}
// for(int i=0; i<dp.length; i++) {
// for(int j=0; j<dp[i].length; j++)
// System.out.print(dp[i][j] + " ");
// System.out.println();
// }
System.out.println("Answer = " + dp[items.length][max_weight]);
}
}