forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRod-Cutting.cpp
76 lines (46 loc) · 1.44 KB
/
Rod-Cutting.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
#include <bits/stdc++.h>
using namespace std;
int cutRodUtil(vector<int>& price, int ind, int N, vector<vector<int>>& dp){
if(ind == 0){
return N*price[0];
}
if(dp[ind][N]!=-1)
return dp[ind][N];
int notTaken = 0 + cutRodUtil(price,ind-1,N,dp);
int taken = INT_MIN;
int rodLength = ind+1;
if(rodLength <= N)
taken = price[ind] + cutRodUtil(price,ind,N-rodLength,dp);
return dp[ind][N] = max(notTaken,taken);
}
int cutRod(vector<int>& price,int N) {
vector<int> cur (N+1,0);
for(int i=0; i<=N; i++){
cur[i] = i*price[0];
}
for(int ind=1; ind<N; ind++){
for(int length =0; length<=N; length++){
int notTaken = 0 + cur[length];
int taken = INT_MIN;
int rodLength = ind+1;
if(rodLength <= length)
taken = price[ind] + cur[length-rodLength];
cur[length] = max(notTaken,taken);
}
}
return cur[N];
}
int main() {
vector<int> price = {2,5,7,8,10};
int n = price.size();
cout<<"The Maximum price generated is "<<cutRod(price,n);
}
/*
Output:
The Maximum price generated is 12
Time Complexity: O(N*N)
Reason: There are two nested loops.
Space Complexity: O(N)
Reason: We are using an external array of size ‘N+1’ to store only one row.
*/
// contributed by Sourav Naskar