Rod Cutting
MediumPython 3Dynamic programmingRecursion
You have a rod of integer length n and a price list: prices[i] is the selling price of a rod piece of length i (prices[0] is 0 and unused). You may cut the rod into any number of integer-length pieces — or not cut it at all — and sell every piece. Return the maximum total revenue.
Constraints: - 0 <= n < len(prices) <= 100 - prices are non-negative integers, prices[0] == 0
Examples: prices = [0, 1, 5, 8, 9, 10, 17, 17, 20] rod_cutting(prices, 4) == 10 # two pieces of length 2: 5 + 5 rod_cutting(prices, 8) == 22 # pieces of lengths 2 and 6: 5 + 17 rod_cutting(prices, 1) == 1 rod_cutting(prices, 0) == 0
Python 3, run in your browser. Run tests runs the problem's own tests with a free account; the AI interviewer reads your code and your runs in a practice interview.
Loading the editor…