-
Notifications
You must be signed in to change notification settings - Fork 2
/
question32.c
61 lines (44 loc) · 1.06 KB
/
question32.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
/*
Given a rod of length 'n' inches and an array of prices that contains prices of all pieces of size smaller
than n, find the max value obtainable by cutting the rod and selling the pieces
There are two methods to solve this:
One is 0/1 knapsack with a twist that is you can include as many as you want
Other is array method as mentioned in geek for geek
*/
//METHOD: 0/1 knapsack with duplicate items
int max(int a, int b){
return a>b?a:b;
}
int cutRod(int *arr, int size){
int length = size;
int dp[size+1][size+1];
int i,j;
for(i=0;i<=size;i++){
for(j=0;j<=size;j++){
dp[i][j] = 0;
}
}
for(i=1;i<=size;i++){
for(j=1;j<=size;j++){
if(i <= j) {
dp[i][j] = max(dp[i-1][j], arr[i-1] + dp[i][j-i]);
}else{
dp[i][j] = dp[i-1][j];
}
}
}
for(i=0;i<=size;i++){
for(j=0;j<=size;j++){
printf("%d ", dp[i][j]);
}
printf("\n");
}
return dp[size][size];
}
int main()
{
int arr[] = {1,5,8,9};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Maximum Obtainable Value is %d\n", cutRod(arr, size));
return 0;
}