-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27_RodCuttin.cpp
188 lines (166 loc) · 4.17 KB
/
27_RodCuttin.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// https://www.codingninjas.com/codestudio/problems/rod-cutting-problem_800284
// Given a rod of length ‘N’ units. The rod can be cut into different sizes and each
// size has a cost associated with it. Determine the maximum cost obtained by cutting
// the rod and selling its pieces.
#include <bits/stdc++.h>
using namespace std;
class Solution5
{
// Tabulation: Space Optimised: Single Array
public:
int cutRod(vector<int> &price, int n)
{
vector<vector<int>> dp(n, vector<int>(n + 1));
vector<int> cp(n + 1);
for (int len = 1; len <= n; ++len)
{
cp[len] = price[0] * len;
}
for (int i = 1; i < n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int cut = INT_MIN;
if (j >= i + 1)
cut = price[i] + cp[j - 1 - i];
int noCut = cp[j];
cp[j] = max(cut, noCut);
}
}
return cp[n];
}
};
class Solution4
{
// Tabulation: Space Optimised
public:
int cutRod(vector<int> &price, int n)
{
vector<vector<int>> dp(n, vector<int>(n + 1));
vector<int> cp(n + 1), xp(n + 1);
for (int len = 1; len <= n; ++len)
{
cp[len] = price[0] * len;
}
for (int i = 1; i < n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int cut = INT_MIN;
if (j >= i + 1)
cut = price[i] + xp[j - 1 - i];
int noCut = cp[j];
xp[j] = max(cut, noCut);
}
cp = xp;
}
return cp[n];
}
};
class Solution3
{
// Tabulation
public:
int cutRod(vector<int> &price, int n)
{
vector<vector<int>> dp(n, vector<int>(n + 1));
for (int len = 1; len <= n; ++len)
{
dp[0][len] = price[0] * len;
}
for (int i = 1; i < n; ++i)
{
for (int j = 0; j <= n; ++j)
{
int cut = INT_MIN;
if (j >= i + 1)
cut = price[i] + dp[i][j - 1 - i];
int noCut = dp[i - 1][j];
dp[i][j] = max(cut, noCut);
}
}
return dp[n - 1][n];
}
};
class Solution2
{
// Recursion: Memoization
public:
int solve(vector<int> &price, vector<vector<int>> &dp, int len, int n)
{
if (n == -1)
return 0;
if (dp[n][len] != -1)
return dp[n][len];
int cut = 0;
if (len >= n + 1)
cut = price[n] + solve(price, dp, len - (n + 1), n);
int noCut = solve(price, dp, len, n - 1);
return dp[n][len] = max(cut, noCut);
}
int cutRod(vector<int> &price, int n)
{
vector<vector<int>> dp(n, vector<int>(n + 1, -1));
return solve(price, dp, n, n - 1);
}
};
class Solution1
{
// Recursion: Optimised
public:
int solve(vector<int> &price, int len, int n)
{
if (n == -1)
return 0;
int cut = 0;
if (len >= n + 1)
cut = price[n] + solve(price, len - (n + 1), n);
int noCut = solve(price, len, n - 1);
return max(cut, noCut);
}
int cutRod(vector<int> &price, int n)
{
return solve(price, n, n - 1);
}
};
class Solution
{
// BruteForce: Recursion
public:
void solve(vector<int> &price, int &ans, int &cost, int &len, int n)
{
// base condition
if (n == 0)
{
ans = max(cost, ans);
return;
}
// Cut
if (len >= n)
{
cost += price[n - 1];
len -= n;
solve(price, ans, cost, len, n);
len += n;
cost -= price[n - 1];
}
// Dont cut
solve(price, ans, cost, len, n - 1);
}
int cutRod(vector<int> &price, int n)
{
int ans = INT_MIN, cost = 0;
solve(price, ans, cost, n, n);
return ans;
}
};
int main()
{
int n = 6;
vector<int> price = {3, 5, 6, 7, 10, 12};
Solution3 Obj1;
cout << Obj1.cutRod(price, n);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}