Skip to content

Commit

Permalink
Merge pull request keon#108 from shivam207/patch-3
Browse files Browse the repository at this point in the history
Create rod_cut.py
  • Loading branch information
keon authored Oct 31, 2017
2 parents 8605aa8 + 5d79250 commit 3ddd39d
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dp/rod_cut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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

0 comments on commit 3ddd39d

Please sign in to comment.