Cut Rod Problem

Bottom-Up Dynamic Programming — step-by-step live visualization

Cut piece — sells for p[i]
Remainder — best revenue already known as r[j−i]
Yellow cell — currently being computed
Green cell — finalized value
Blue cell — value being read (r[j−i])
Purple cell — optimal first cut stored in s[j]
Rod Visualization
DP Table
0 / 0
Optimal Solution
Current Step
Press ▶ Play to begin…
Algorithm
def bottom_up_cut_rod(p, n): r = [0] * (n + 1) # r[0]=0 for j in range(1, n+1): q = -∞ for i in range(1, j+1): q = max(q, p[i] + r[j-i]) r[j] = q return r[n]
Price Table (p[i])

The Problem

You have a rod of length n inches and a price table p[1..n] where p[i] is what a piece of length i sells for. You can cut the rod into any combination of integer lengths. The goal is to maximize total revenue.


A rod of length 4 sold whole earns $9, but two pieces of length 2 earn $5 + $5 = $10. Finding the best combination by brute force takes O(2ⁿ) time — dynamic programming reduces this to O(n²).

How the Algorithm Works

Reading the Visualization