Best Revenue from Selling Rod Segments
Dynamic ProgrammingAuto-graded · 22 test casesLast updated
Tests dynamic programming by finding the optimal way to partition a rod into segments to maximize total selling price.
You have a rod measuring rod_length meters. You also have a list segment_prices where segment_prices[i] represents how much you can sell a segment of exactly i + 1 meters for.
You need to decide how to partition the rod into segments (each an integer number of meters) to maximize your total revenue. You may cut the rod into any number of pieces, or sell it uncut.
Write a function that returns the maximum revenue achievable.
Constraints
1 <= rod_length <= 1000len(segment_prices) == rod_length1 <= segment_prices[i] <= 10^5
Examples
Example 1
Input: best_rod_revenue(4, [1, 5, 8, 9])
Output: 10
Example 2
Input: best_rod_revenue(8, [3, 5, 8, 9, 10, 17, 17, 20])
Output: 24
Hints
Hint 1
Consider what happens when you decide the length of the first segment you sell—what problem remains?
Hint 2
Build a table where entry i stores the best revenue for a rod of length i, using previously computed smaller lengths.
Hint 3
For each length, try all possible first-segment choices and take the maximum revenue.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem exhibits optimal substructure: if you decide to cut off and sell a segment of length k first, the remaining rod of length rod_length - k should be handled optimally to maximize total revenue.
We use dynamic programming with a bottom-up approach. Define revenue[i] as the maximum revenue obtainable from a rod of length i. The recurrence is:
revenue[0] = 0
revenue[i] = max over all k in [1, i] of (segment_prices[k-1] + revenue[i-k])
For each length i, we consider every possible choice for the first segment we sell (lengths 1 through i), compute the revenue from that segment plus the optimal solution for what remains, and take the maximum.
When k = i, this represents selling the entire rod without cutting, which is always one valid option.
Why It Works
The key insight is that any valid partitioning has some first segment. If we try all possible first segments and recursively solve the remainder optimally, we explore all valid partitions. The dynamic programming table ensures we solve each subproblem only once, building from smaller lengths to larger ones so all dependencies are ready when needed.
Segments can repeat (for example, three segments of length 2), which is naturally handled because when computing revenue[i - k], that subproblem may itself have used length k. This is the unbounded knapsack property.
Complexity
Time complexity: O(rod_length²). For each length from 1 to rod_length, we iterate through all possible first-segment lengths, resulting in roughly (rod_length² / 2) operations.
Space complexity: O(rod_length) for the dynamic programming table.
Solution Code
def best_rod_revenue(rod_length: int, segment_prices: list[int]) -> int:
# revenue[i] = maximum revenue obtainable from a rod of length i
revenue = [0] * (rod_length + 1)
for current_length in range(1, rod_length + 1):
max_revenue = 0
# Try each possible length for the first segment
for first_segment in range(1, current_length + 1):
max_revenue = max(
max_revenue,
segment_prices[first_segment - 1] + revenue[current_length - first_segment]
)
revenue[current_length] = max_revenue
return revenue[rod_length]