Skip to content

Commit 8a1c950

Browse files
authored
Create minimum-cost-to-cut-a-stick.py
1 parent 82f2be9 commit 8a1c950

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/minimum-cost-to-cut-a-stick.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(n^3)
2+
# Space: O(n^2)
3+
4+
class Solution(object):
5+
def minCost(self, n, cuts):
6+
"""
7+
:type n: int
8+
:type cuts: List[int]
9+
:rtype: int
10+
"""
11+
sorted_cuts = sorted(cuts + [0, n])
12+
dp = [[0]*len(sorted_cuts) for _ in xrange(len(sorted_cuts))]
13+
for l in xrange(2, len(sorted_cuts)):
14+
for i in xrange(len(sorted_cuts)-l):
15+
dp[i][i+l] = min(dp[i][j]+dp[j][i+l] for j in xrange(i+1, i+l)) + \
16+
sorted_cuts[i+l]-sorted_cuts[i]
17+
return dp[0][len(sorted_cuts)-1]

0 commit comments

Comments
 (0)