打卡记录
多边形三角剖分的最低得分(区间DP)
python
class Solution:
def minScoreTriangulation(self, values: List[int]) -> int:
n = len(values)
f = [[0] * n for _ in range(n)]
for i in range(n - 3, -1, -1):
for j in range(i + 2, n):
f[i][j] = min(f[i][k] + f[k][j] + values[i] * values[j] * values[k]
for k in range(i + 1, j))
return f[0][n - 1]
合并石头的最低成本(区间DP)
python
class Solution:
def mergeStones(self, stones: List[int], k: int) -> int:
n = len(stones)
if (n - 1) % (k - 1):
return -1
s = list(accumulate(stones, initial=0))
f = [[0] * n for _ in range(n)]
for i in range(n - 1, -1, -1):
for j in range(i + 1, n):
f[i][j] = min(f[i][m] + f[m + 1][j] for m in range(i, j, k - 1))
if not (j - i) % (k - 1):
f[i][j] += s[j + 1] - s[i]
return f[0][-1]