Maximize revenue by cutting a rod into priced integer-length pieces

Quick Overview

Find the maximum revenue from cutting a rod of length n into integer-length pieces, given a price for every piece length and unlimited reuse of each length. It tests unbounded dynamic programming, building a recurrence over the first cut, and careful indexing of a 1-based price list.

Maximize revenue by cutting a rod into priced integer-length pieces

Company: Zoho

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer array `prices` of length `n`, where `prices[i - 1]` is the selling price of a rod piece of integer length `i`, return the maximum revenue obtainable by cutting a rod of length `n` into pieces whose lengths sum exactly to `n`. You may use any piece length multiple times, and you may also leave the rod uncut. There is no cost for making a cut. ### Function Signature ```python def rod_cutting(prices: list[int]) -> int: ``` ### Rules - The rod length is `n = len(prices)`. A piece of length `i` (for `1 <= i <= n`) sells for `prices[i - 1]`. - Every piece has a positive integer length, and the piece lengths must add up to exactly `n`. - Return the maximum revenue as an integer. The cuts themselves are not part of the output. ### Constraints - `1 <= n == len(prices) <= 1000` - `0 <= prices[i] <= 10⁵` ### Examples ```text Input: prices = [1,5,8,9] Output: 10 ``` Cutting the rod of length 4 into two pieces of length 2 gives 5 + 5 = 10. The other ways earn less: selling it whole or as 3 + 1 earns 9, 2 + 1 + 1 earns 7, and four pieces of length 1 earn 4. ```text Input: prices = [3] Output: 3 ``` The rod has length 1 and can only be sold whole for 3. ```text Input: prices = [1,5,8,9,10,17,17,20] Output: 22 ``` Cutting the rod of length 8 into pieces of length 2 and 6 gives 5 + 17 = 22. Selling it whole earns only 20, and 3 + 3 + 2 earns 21.

Overview: Find the maximum revenue from cutting a rod of length n into integer-length pieces, given a price for every piece length and unlimited reuse of each length. It tests unbounded dynamic programming, building a recurrence over the first cut, and careful indexing of a 1-based price list.

Zoho logo
Zoho
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer array prices of length n, where prices[i - 1] is the selling price of a rod piece of integer length i, return the maximum revenue obtainable by cutting a rod of length n into pieces whose lengths sum exactly to n.

You may use any piece length multiple times, and you may also leave the rod uncut. There is no cost for making a cut.

Function Signature

def rod_cutting(prices: list[int]) -> int:

Rules

  • The rod length is n = len(prices) . A piece of length i (for 1 <= i <= n ) sells for prices[i - 1] .
  • Every piece has a positive integer length, and the piece lengths must add up to exactly n .
  • Return the maximum revenue as an integer. The cuts themselves are not part of the output.

Constraints

  • 1 <= n == len(prices) <= 1000
  • 0 <= prices[i] <= 10⁵

Examples

Input: prices = [1,5,8,9]
Output: 10

Cutting the rod of length 4 into two pieces of length 2 gives 5 + 5 = 10. The other ways earn less: selling it whole or as 3 + 1 earns 9, 2 + 1 + 1 earns 7, and four pieces of length 1 earn 4.

Input: prices = [3]
Output: 3

The rod has length 1 and can only be sold whole for 3.

Input: prices = [1,5,8,9,10,17,17,20]
Output: 22

Cutting the rod of length 8 into pieces of length 2 and 6 gives 5 + 17 = 22. Selling it whole earns only 20, and 3 + 3 + 2 earns 21.