forked from abhimanyuiscoding/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rod_cutting_CPP
50 lines (36 loc) · 1.07 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
//PROBLEM-> Given a rod of length n and a list of rod prices of length i, where 1 <= i <= n, find the optimal way to cut the rod into smaller rods to maximize profit.
//MY SOLUTION->
#include <iostream>
#include <string>
#include <climits>
using namespace std;
// Function to find the best way to cut a rod of length `n`
// where the rod of length `i` has a cost `price[i-1]`
int rodCut(int price[], int n)
{
// base case
if (n == 0) {
return 0;
}
int maxValue = INT_MIN;
// one by one, partition the given rod of length `n` into two parts
// of length (1, n-1), (2, n-2), (3, n-3), … ,(n-1, 1), (n, 0)
// and take maximum
for (int i = 1; i <= n; i++)
{
// rod of length `i` has a cost `price[i-1]`
int cost = price[i - 1] + rodCut(price, n - i);
if (cost > maxValue) {
maxValue = cost;
}
}
return maxValue;
}
int main()
{
int price[] = { 1, 5, 8, 9, 10, 17, 17, 20 };
// rod length
int n = 4;
cout << "Profit is " << rodCut(price, n);
return 0;
}