How can we modify the CUT-ROD algorithm that we studied in class so that it impo
ID: 3741563 • Letter: H
Question
How can we modify the CUT-ROD algorithm that we studied in class so that it imposes a fixed cost per cut when partitioning the rod into smaller pieces? Fill in each of the three boxes below with Java code that correctly completes the integer function bottomDpCutRodInclCutCost intl P, int n, int costPerCut) which when passed: (1) int array P[O..n] (where p[i] is the price for which an i-inch-long piece of rod can be sold); (2) int n (the total length in inches of a rod available for cutting); and (3) int costPerCut (a fixed cost incurred once for each cut made in the rod) returns the maximum net revenue (total sale price minus total cutting cost) that can be obtained by selling the rod after partitioning/cutting it into an optimally chosen set of smaller pieces (each of which has length equal to some positive integer number of inches). You should assume that pto]-o;that P[A] 0 for all 0Explanation / Answer
ANSWER:
Public static int bottomUpCutRodInclCutCost(int[]p,int n,int cost per cut
{
Int[] pMinusCutcost=new int[n+1];
Int[] x=new int[n+1];
For(int k=1;k<=n;k++)
pMinusCutCost[k]= p[I]-costPerCut;
x[0]=0;
for(int j=1;j<=n;j++;
{
Int q=-1;
For {int i=1;i<=j;i++)
q=max(q, q = max(q, p[i] + r[j-i-1]);
r[j]=q;
}
return q
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.