Problem Statement

Some company buys long steel rods and cuts them into shorter rods, which it then sells.

Each cut is free. The management of company wants to know the best way to cut up the rods.

We assume that we know the price pi in dollars (for i = 1, 2,…n ), that the company charges for a rod of length i inches. Rod lengths are always an integral number of inches.

Goal

Determine the maximum revenue rn , obtainable by cutting up the rod and selling the pieces

Example

Length i12345678910
Price pi1589101717202430

CleanShot 2023-10-15 at 10.20.19@2x

The most optimal revenue will be deduced by cutting the rod into 2 equal cuts: 5+5 = 10

Brute Force Solution

We will explore each possibility of cutting the rod into various sizes

This can be defined from the recurrence:

  • Cut a piece of length i , with remainder of length n

  • Only the remainder, and not the rest piece, may be further divided

    • Hence only the remainder is being used to check the optimal solution of rest of the length

CleanShot 2023-10-15 at 10.41.21@2x

This Solution will give us a Time Complexity of T(2n )

This is because every recursive call itself produce further n-1 recursive calls

CleanShot 2023-10-15 at 10.50.25@2x

Dynamic Programming Solution

Optimal Substructure

  • The Optimal Solution / split of the current length can also have an optimal solution

CleanShot 2023-10-15 at 10.31.32@2x

Memoization

  • We can avoid repetitions through memoization

CleanShot 2023-10-15 at 10.51.38@2x

To implement this we can just change the notation of our BruteForce Recurrence

We can store the result / revenue r​ into an array

Bottom Up DP Solution

CleanShot 2023-10-15 at 11.00.25@2x

Where C is our array r​ and V is the array p

This give us a Time Complexity of