-
Notifications
You must be signed in to change notification settings - Fork 1
/
11_RodCutting.cpp
49 lines (43 loc) · 1.28 KB
/
11_RodCutting.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
// Note :- https://www.geeksforgeeks.org/cutting-a-rod-dp-13/
// The first approach that comes to our mind is greedy. A greedy solution will fail in this problem
// because there is no ‘uniformity’ in data. While selecting a local better choice we may choose an
// item that will in long term give less value.
// As the greedy approach doesn’t work, we will try to generate all possible combinations using
// recursion and select the combination which gives us the maximum value in the given constraints.
#include <bits/stdc++.h>
using namespace std;
int getMaxProfit(int length[], int price[], int n, int L) {
int t[n + 1][L + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= L; j++) {
if (j == 0 || i == 0) {
t[i][j] = 0;
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= L; j++) {
if (length[i - 1] <= j) {
t[i][j] = max(price[i - 1] + t[i][j - length[i - 1]], t[i - 1][j]);
}
else {
t[i][j] = t[i - 1][j];
}
}
}
return t[n][L];
}
signed main() {
int n; cin >> n;
int length[n], price[n];
for (int i = 0; i < n; i++) {
cin >> length[i];
}
for (int i = 0; i < n; i++) {
cin >> price[i];
}
int L;
cin >> L;
cout << getMaxProfit(length, price, n, L) << endl;
return 0;
}