-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuttingARod.cpp
More file actions
63 lines (62 loc) · 1.29 KB
/
CuttingARod.cpp
File metadata and controls
63 lines (62 loc) · 1.29 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
60
61
62
63
#include<iostream>
using namespace std;
//Time Complexity is O(n^2) And Space Complexity is O(n^2)
int cutARod(int *pr,int n)
{
int **dp=new int*[n+1];
for(int i=0;i<=n;i++)
{
dp[i]=new int[n+1];
}
for(int i=0;i<=n;i++)
{
dp[0][i]=0;
dp[i][0]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i>j)
{
dp[i][j]=dp[i-1][j];
}
else
{
dp[i][j]=max(pr[i-1]+dp[i][j-i],dp[i-1][j]);
}
}
}
return dp[n][n];
}
//Time Complexity is O(n^2) And Space Complexity is O(n)
int cutARod2(int *pr,int n)
{
int *dp=new int[n+1];
for(int i=0;i<=n;i++)
{
dp[i]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
dp[j]=max(pr[i-1]+dp[j-i],dp[j]);
}
}
return dp[n];
}
int main()
{
int n;
cout<<"Enter the length:\n";
cin>>n;
int *price=new int[n];
cout<<"Enter the prices :\n";
for(int i=0;i<n;i++)
{
cin>>price[i];
}
cout<<"The maximum Value Obtained in O(n^2) = "<<cutARod(price,n);
cout<<"The maximum Value Obtained in O(n) = "<<cutARod2(price,n);
}