-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRod Cutting.c
69 lines (57 loc) · 1.43 KB
/
Rod Cutting.c
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
// complexity O(n^n)
// could be memoized (bottom up approach)
#include <stdio.h>
#include <string.h>
#define MAX(a,b) a > b ? a : b
#define ELEMENTS (8)
int MaxProfit(int *len, int *price, int n, int rod_length)
{
if (rod_length == 0)
{
// Empty string
return 0;
}
else
{
int max_profit = 0;
for (int i=0; i<n; i++)
{
if (rod_length >= len[i])
{
int profit = price[i] + MaxProfit(len, price, n, rod_length-len[i]);
if (profit > max_profit)
{
max_profit = profit;
}
}
}
return max_profit;
}
}
int main()
{
int len[ELEMENTS] = {1,2,3,4,5,6,7,8};
int price[ELEMENTS] = {1,5,8,9,10,17,17,20};
int rod_length = 4;
printf("Max Profit = %u\n",MaxProfit(len, price, ELEMENTS, rod_length));
return 0;
}
// Bottoms up reference
int rodCut(int price[], int n)
{
// T[i] stores maximum profit achieved from rod of length i
int T[n + 1];
// initialize maximum profit to 0
for (int i = 0; i <= n; i++)
T[i] = 0;
// consider rod of length i
for (int i = 1; i <= n; i++)
{
// divide the rod of length i into two rods of length j
// and i-j each and take maximum
for (int j = 1; j <= i; j++)
T[i] = max(T[i], price[j - 1] + T[i - j]);
}
// T[n] stores maximum profit achieved from rod of length n
return T[n];
}