Rod Cutting

Given a rod of length n and a price array where price[i] is the value of a rod piece of length i+1, return the maximum obtainable value by cutting and selling the rod pieces. Pieces can be used repeatedly, making this an unbounded knapsack maximization problem.

Input Format

prices = value of pieces of length 1..n, n = rod length

Output Format

maximum value obtainable by cutting and selling the rod

Constraints

  • 1 <= n <= 1000; 1 <= prices.length == n; 1 <= prices[i] <= 10^5

Examples

Example 1:

Input:

prices = [1,5,8,9,10,17,17,20]
n = 8

Output:

22

Explanation:

The maximum revenue for length 8 is 22.

Example 2:

Input:

prices = [2,5,7,8]
n = 4

Output:

10

Explanation:

Cut the rod into two pieces of length 2 for value 5 + 5 = 10.

Loading...
Rod Cutting - Dp Knapsack DSA Problem