Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n.
https://www.youtube.com/watch?v=IRwVmTmN6go
Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 1 5 8 9 10 17 17 20
And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1)
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 3 5 8 9 10 17 17 20
# A Dynamic Programming solution for Rod cutting problem
INT_MIN = -32767
# Returns the best obtainable price for a rod of length n and
# price[] as prices of different pieces
def cutRod(price, n):
val = [0 for x in range(n+1)]
val[0] = 0
# Build the table val[] in bottom up manner and return
# the last entry from the table
for i in range(1, n+1):
max_val = INT_MIN
for j in range(i):
max_val = max(max_val, price[j] + val[i-j-1])
val[i] = max_val
return val[n]
# Driver program to test above functions
arr = [1, 5, 8, 9, 10, 17, 17, 20]
size = len(arr)
print("Maximum Obtainable Value is " + str(cutRod(arr, size)))
# This code is contributed by Bhavya Jain
Time Complexity of the above implementation is O(n^2) which is much better than the worst case time complexity of Naive Recursive implementation.
Naive recursive => exponential.
Modify MEMOIZED-CUT-ROD to return not only the value but the actual solution, too.
def cut_rod_sub(p, n, r, s):
if r[n] >= 0:
return r[n]
r[n] = 0
for i in range(1, n + 1):
ret = p[i] + cut_rod_sub(p, n - i, r, s)
if r[n] < ret:
r[n] = ret
s[n] = i
return r[n]
def cut_rod(p, n):
r = [-1 for _ in xrange(n + 1)]
s = [i for i in xrange(n + 1)]
cut_rod_sub(p, n, r, s)
r = r[n]
subs = []
while n > 0:
subs.append(s[n])
n -= s[n]
return r, subs