-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_knapsack_top_down.cpp
More file actions
56 lines (50 loc) · 1.15 KB
/
01_knapsack_top_down.cpp
File metadata and controls
56 lines (50 loc) · 1.15 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
//Problem Statement : https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1
// this code describes the top-down version and I have write it's other two solutions where I will be showing the complete recursive version
// as well as the memosised version
//I will not be displaying any prompt throughout this code
#include<iostream>
using namespace std;
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int knapsack(int wt[],int value[],int n,int w)
{
int **dp=new int*[n+1];
for(int i=0;i<n+1;i++)
{
dp[i]=new int[w+1];
for(int j=0;j<w+1;j++)
dp[i][j]=0;
}
for(int i=1;i<n+1;i++)
{
for(int j=0;j<w+1;j++)
{
if(wt[i-1]<=j)
{
dp[i][j]=max(value[i-1]+dp[i-1][j-wt[i-1]],dp[i-1][j]);
}
else
dp[i][j]=dp[i-1][j];
}
}
return dp[n][w];
}
int main()
{
int n;
int w;
cin>>n;
cin>>w;
int wt[n];
int value[n];
for(int i=0;i<n;i++)
cin>>value[i];
for(int i=0;i<n;i++)
cin>>wt[i];
cout<<"\nTotal Profit is : "<<knapsack(wt,value,n,w);
}