Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created September 29, 2025 21:53
Show Gist options
  • Select an option

  • Save Ifihan/3c8b44504184135625c6cbf60780c392 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/3c8b44504184135625c6cbf60780c392 to your computer and use it in GitHub Desktop.
Minimum Score Triangulation of Polygon

Question

Approach

I treat the polygon as an interval [i, j] of vertices. If I want to triangulate that section, I pick a third vertex k between i and j to form triangle (i, k, j). The cost is values[i] * values[k] * values[j] + dp[i][k] + dp[k][j]. I compute the minimum across all choices of k.

Implementation

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

Complexities

  • Time: O(n^2)
  • Space: O(n^2)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment