R (I) : The Optimal Revenue While Cutting A Length I Rod
R (I) : The Optimal Revenue While Cutting A Length I Rod
Programming Class-work
We have a rod of length n units. We wish to cut it up into pieces and sell them. For
example, if n=5, we could cut the rod like 1+1+1+1+1, or 2+2+1, or 3+2, or 3+1+1,
4+1, 5, etc. However, each piece-length has a different price. Therefore, the total
revenue generated by a cutting may be different from the total revenue generated by
a different cutting. For example, look at the table below. In this case n=10. If we cut
the rod as 5+5 we get a revenue of 270; if we cut it as 9+1, we get a revenue of 230; if
we sell the entire length 10 piece we get a revenue of 235 etc. The prices for all
piece-lengths are given to us in input. We wish to Pind a cutting that maximises the
total revenue.
An optimal sub-structure for the rod cutting problem may be dePined as follows.
i 1 2 3 4 5 6 7 8 9 10
r
prev[i]
(iii) What do you store in prev[i], the optimal cut for a rod of length i?
(iv) Write the bottom-up DP code to compute r[i] and c[i]. Add the code that uses
r and c to return the optimal revenue and corresponding cutting.