Bottom-Up Dynamic Programming — step-by-step live visualization
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²).
i = 1 … j. The candidate revenue is p[i] + r[j−i]: sell the first piece and optimally cut the rest.r[j] holds the maximum over all cuts. s[j] records which cut produced that maximum — used later to reconstruct the actual pieces.s[n] → s[n−s[n]] → … to read off the optimal cut sequence.i); the blue segment is the remainder whose best revenue is already in the table.