Skip to content
StackDecode
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

  1. A rod of length n can be cut into pieces; each length has a price. Maximise the total.
  2. best(n) = max over first-piece length j of price[j] + best(n − j).
  3. Fill best(0), best(1), best(2)… in order.
  4. O(n²).

Watch it run

Loading animation…