SDE & MLE Prep

1039. Minimum Score Triangulation of Polygon

Problem

A convex polygon's vertices, in clockwise order, have the integer values. Split it into n − 2 triangles; each triangle scores the product of its three vertex values. Return the minimum possible total score.

Example: [1,2,3] → 6; [3,7,4,5] → 144

Constraints: n ≤ 50.

Edge (i, j) belongs to exactly one triangle (i, k, j). dp[i][j] = min over k of dp[i][k] + v[i]·v[k]·v[j] + dp[k][j], filled by increasing gap.

Complexity: O(n³).

class Solution:
    def minScoreTriangulation(self, values: List[int]) -> int:
        n = len(values)
        dp = [[0] * n for _ in range(n)]
        for gap in range(2, n):
            for i in range(n - gap):
                j = i + gap
                dp[i][j] = min(dp[i][k] + values[i] * values[k] * values[j] + dp[k][j] for k in range(i + 1, j))
        return dp[0][n - 1]