Problem Description
A company buys long steel rods and cuts them for sale. Cutting costs are negligible. Rod lengths are in inches. Given a price table $p$, where $p_{i}(i=1,2,\cdots,m)$ represents the price of a rod of length $i$ inches, find the cutting scheme that maximizes total revenue.
Problem Analysis
Assume the length of the long steel rod is $n$ inches. If the leftmost cut of the optimal scheme is $i$ inches long, we then solve for the optimal cutting scheme of the remaining $n-i$ inches. By considering all possible values for $i$, the maximum revenue $r_{n}$ corresponding to the best cutting scheme can be defined recursively:
$$ r_{n}=max_{1\le i \le n}(p_{i}+r_{n-i}) $$C Code
There are two approaches to this problem:
(1) Divide and Conquer (Top-Down)
| |
Time complexity $O(2^{n})$
(2) Dynamic Programming (Bottom-Up)
| |
Time complexity $O(n^{2})$
Other Resources
Found an existing article during my search: 【基础算法】动态规划详解——钢条切割