Skip to content
LC-1547 Hard LeetCode

1547. Minimum Cost to Cut a Stick

Read the full problem statement on LeetCode.
Difficulty: hard Acceptance: 62% Topics: Array, Dynamic Programming, Sorting
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n^3)
# Space: O(n^2)

class Solution(object):
    def minCost(self, n, cuts):
        """
        :type n: int
        :type cuts: List[int]
        :rtype: int
        """
        sorted_cuts = sorted(cuts + [0, n])
        dp = [[0]*len(sorted_cuts) for _ in xrange(len(sorted_cuts))]
        for l in xrange(2, len(sorted_cuts)):
            for i in xrange(len(sorted_cuts)-l):
                dp[i][i+l] = min(dp[i][j]+dp[j][i+l] for j in xrange(i+1, i+l)) + \
                             sorted_cuts[i+l]-sorted_cuts[i]
        return dp[0][len(sorted_cuts)-1]

Solution from kamyu104/LeetCode-Solutions · MIT