data-structures · intermediate · ~15 min
Maximize revenue by cutting a rod into pieces.
Implement:
int rod_cutting(const int *price, int n);
Return the maximum revenue obtainable by cutting a rod of length n, where price[k] is the price of a piece of length k+1.
price[0..n-1], rod length n.
Maximum revenue.
Unbounded: each length may be cut many times.
#include <stddef.h>
/* Max revenue cutting a rod of length n, where price[k] is the price of a piece of length k+1. */
int rod_cutting(const int *price,int n){ (void)price;(void)n; return 0; }
Off-by-one indexing between piece length and its price index.
Length 0 -> 0 revenue.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.