forked from the-moonLight0/Hactober-fest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0 - 1 Knapsack Problem(Java)
75 lines (50 loc) · 1.57 KB
/
0 - 1 Knapsack Problem(Java)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Top-Dpwn Approach(Memoization):---
class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int knapSack(int W, int wt[], int val[], int n,int [][]t)
{
// your code here
if(n==0 || W==0)
return 0;
if(t[n][W]!=-1)
return t[n][W];
if(wt[n-1] > W)
return t[n][W]= knapSack(W,wt,val,n-1,t);
else
return t[n][W]=Math.max(val[n-1]+knapSack(W-wt[n-1],wt,val,n-1,t),knapSack(W,wt,val,n-1,t));
}
static int knapSack(int W, int wt[], int val[], int n)
{
int[][] t = new int[n+1][W+1];
for(int i = 0; i < n + 1 ; i++)
for(int j = 0; j < W + 1; j++)
t[i][j] = -1;
return knapSack(W, wt, val, n,t );
}
}
Bottom-up Approach(Tabulation):---
class Solution
{
//Function to return max value that can be put in knapsack of capacity W.
static int knapSack(int W, int wt[], int val[], int n)
{
// your code here
int[][] t = new int[n+1][W+1];
for(int i = 0; i < n + 1; i++){
for(int j = 0; j < W + 1; j++) {
if(i==0 || j==0)
t[i][j] = 0;
}
}
for(int i = 1; i < n + 1; i++){
for(int j = 1; j < W + 1; j++) {
if(wt[i-1] <= j)
t[i][j]=Math.max(val[i-1]+ t[i-1][j-wt[i-1]],t[i-1][j]);
else
t[i][j]= t[i-1][j];
}
}
return t[n][W];
}
}