-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknapsack.java
More file actions
59 lines (48 loc) · 1.68 KB
/
knapsack.java
File metadata and controls
59 lines (48 loc) · 1.68 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
package DynamicProgramming;
public class knapsack {
//Recursion + Memoization T.C & A.S ==> O(n * C)
public static int profit(int i , int[] wt , int[] val , int C , int[][] dp){
if( i == wt.length )
return 0 ;
if(dp[i][C] != -1)
return dp[i][C] ;
int skip = profit(i + 1, wt, val, C , dp);
if(wt[i] > C)
return dp[i][C] = skip ;
int take = val[i] + profit(i+1, wt, val, C-wt[i] , dp) ;
return dp[i][C] = Math.max(take, skip) ;
}
public static void main(String[] args) {
int[] val = { 5 , 3 , 7 , 16} ;
int[] wt = { 1 , 2 , 8 , 10} ;
int C = 8 ;
int n = wt.length ;
// i = 0 to n-1 | C = C to 0
int[][] dp = new int[n][C+1] ;
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1 ;
}
}
System.out.println(profit(0 , wt, val, C , dp));
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
dp[i][j] = -1;
}
}
// Tabulation
for(int i = 0 ; i < dp.length ; i++){
for(int j = 0 ; j < C+1 ; j++){
int skip = (i > 0) ? dp[i-1][j] : 0 ;
if (wt[i] > j)
dp[i][j] = skip ;
else{
int take = val[i];
take += (i > 0) ? dp[i - 1][j - wt[i]] : 0;
dp[i][j] = Math.max(take, skip);
}
}
}
System.out.println(dp[n-1][C]);
}
}