Rod Cutting Problem
Build a recursive solution for a commonly asked coding interview problem.
We'll cover the following...
Problem statement
You are given a rod of size n > 1
, it can be cut into any number of pieces k
. The price for each piece of size i
is represented as price(i)
and the maximum revenue from a rod of size i
is r(i)
(could be split into multiple pieces). Find r(n)
for the rod of size n
.
Consider that the length of the rod is 6
.
The price of size is given below:
Length | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
Price(p) | 2 | 5 | 8 | 9 | 10 | 11 |
The possible number of rods after cutting can give the following price:
Rod(6) | 1*6 | 1*4, 2 | 1*3, 3 | 1, 2, 3 | 1*2, 4 | 1, 5 | 2*3 | 2, 4 | 3, 3 |
---|---|---|---|---|---|---|---|---|---|
Price(p) | 12 |