Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Rod Cutting

Medium Acceptance 50.00% Points 30.00

Given a rod of length n(size of array) inches and an array price[] . price[i] denotes the value of a piece of length i. The task is to determine the maximum value obtainable by cutting up the rod and selling the pieces.

Examples
Example 1

Example 1

Input: price[] = [3, 5, 8, 9, 10, 17, 17, 20]

Output: 24

Explanation: The maximum obtainable value is 24 by cutting the rod into 8 pieces of length 1, i.e, 8*price[1]= 8*3 = 24.

Example 2

Example 2

Input: price[] = [1, 10, 3, 1, 3, 1, 5, 9]

Output: 40

Explanation: The maximum obtainable value is 40 by cutting the rod into 4 pieces of length 2, i.e, 4*price[2]= 4*10 = 40

Hints
Hint 1
Expected Time Complexity: O(n * n)
Hint 2
Expected Auxiliary Space: O(n * n)
Constraints
  • 1 <= price.size() <= 10^3
  • 1 <= price[i] <= 10^6
Companies
Google
Topics
Array Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions