Minimum Cost to Cut a Stick LeetCode solution in Python
1class Solution:2def minCost(self, n: int, cuts: List[int]) -> int:3cuts = [0] + sorted(cuts) + [n]4k = len(cuts)5dp = [[float("inf")] * k for _ in range(k)]6for l in range(1, k + 1):7for beg in range(k - l):8end = beg + l9if l == 1:10dp[beg][end] = 011continue12for i in range(beg + 1, end):13currcost = cuts[end] - cuts[beg]14currcost += dp[beg][i] + dp[i][end]15dp[beg][end] = min(dp[beg][end], currcost)16return dp[0][k - 1]