Skip to content

Commit 216ef4e

Browse files
Merge pull request #547 from vidhanjhawar/master
added rod cutting problem in dynamic programming
2 parents 05c99c9 + 268ec6a commit 216ef4e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Oct 23 21:49:30 2019
4+
5+
@author: Lenovo
6+
"""
7+
8+
INT_MIN = -999999
9+
10+
# Returns the best obtainable price for a rod of length n and
11+
# price[] as prices of different pieces
12+
def cutRod(price, n):
13+
val = [0 for x in range(n+1)]
14+
val[0] = 0
15+
16+
# Build the table val[] in bottom up manner and return
17+
# the last entry from the table
18+
for i in range(1, n+1):
19+
max_val = INT_MIN
20+
for j in range(i):
21+
max_val = max(max_val, price[j] + val[i-j-1])
22+
val[i] = max_val
23+
24+
return val[n]
25+
26+
# Driver program to test above functions
27+
arr = [1, 5, 8, 9, 10, 17, 17, 20]
28+
size = len(arr)
29+
print("Maximum Obtainable Value is " + str(cutRod(arr, size)))

0 commit comments

Comments
 (0)