Dynamic programmingAnimated
Rod Cutting
Best revenue from cutting a rod into priced pieces.
Time
O(n²)
Extra space
O(n)
Use it for
Unbounded knapsack in disguise.
How it works
- A rod of length n can be cut into pieces; each length has a price. Maximise the total.
- best(n) = max over first-piece length j of price[j] + best(n − j).
- Fill best(0), best(1), best(2)… in order.
- O(n²).
Watch it run
Loading animation…