-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP-knapsackZeroOne.cpp
100 lines (74 loc) · 1.69 KB
/
DP-knapsackZeroOne.cpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
// recursive +memoization
int solve(int W, int wt[], int val[], int n, int idx)
{
if (idx >= n)
{
return 0;
}
if (W == 0)
{
return 0;
}
if (dp[idx][W] != -1)
{
return dp[idx][W];
}
if (W >= wt[idx])
{
return dp[idx][W] = max(val[idx] + solve(W - wt[idx], wt, val, n, idx + 1),
solve(W, wt, val, n, idx + 1));
}
else if (W < wt[idx])
{
return dp[idx][W] = solve(W, wt, val, n, idx + 1);
}
}
// tabulation top-down
int knapSack(int W, int wt[], int val[], int n)
{
int dp[n + 1][W + 1];
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= W; j++)
{
if (i == 0 or j == 0)
dp[i][j] = 0;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= W; j++)
{
if (wt[i - 1] <= j)
{
dp[i][j] = max(dp[i - 1][j], val[i - 1] + dp[i - 1][j - wt[i - 1]]);
}
else
{
dp[i][j] = dp[i - 1][j];
}
}
}
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= W; j++)
{
cout << dp[i][j] << " ";
}
cout << "\n";
}
return dp[n][W];
}
int main()
{
int n = 3, W = 4;
// cin>>n>>W;
int wt[n] = {4, 5, 1};
int val[n] = {1, 2, 3};
// cout << knapSack(W, wt, val, n) << "\n";
memset(dp,-1,sizeof(dp));
cout<<solve(W,wt,val,n,0)<<"\n";
}