Minimum Score Triangulation of Polygon - Problem

You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the i-th vertex in clockwise order.

Polygon triangulation is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in n - 2 triangles.

For each triangle, the weight of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these weights over all n - 2 triangles.

Return the minimum possible score that you can achieve with some triangulation of the polygon.

Input & Output

Example 1 — Basic Quadrilateral
$ Input: values = [1,2,3,4]
Output: 18
💡 Note: We can triangulate as (0,1,2) and (0,2,3): cost = 1×2×3 + 1×3×4 = 6 + 12 = 18. Or as (0,1,3) and (1,2,3): cost = 1×2×4 + 2×3×4 = 8 + 24 = 32. The minimum is 18.
Example 2 — Triangle
$ Input: values = [2,1,3]
Output: 6
💡 Note: With only 3 vertices, there's only one triangle possible: (0,1,2) with cost 2×1×3 = 6.
Example 3 — Pentagon
$ Input: values = [3,7,4,5]
Output: 144
💡 Note: For a quadrilateral with values [3,7,4,5], we can triangulate in different ways. One optimal way gives cost 144.

Constraints

  • n == values.length
  • 3 ≤ n ≤ 50
  • 1 ≤ values[i] ≤ 100

Visualization

Tap to expand
Minimum Score Triangulation of Polygon INPUT 1 2 3 4 v0 v1 v2 v3 Input Array: 1 2 3 4 n = 4 vertices Need n-2 = 2 triangles Convex polygon in clockwise order ALGORITHM STEPS 1 Define DP State dp[i][j] = min score for polygon from i to j 2 Recurrence dp[i][j] = min(dp[i][k] + dp[k][j] + v[i]*v[k]*v[j]) 3 Try All k Values For each k between i,j pick vertex for triangle 4 Build Solution Answer = dp[0][n-1] DP Table (partial): i\j 2 3 0 6 18 1 - 24 dp[0][3] = min score for full polygon FINAL RESULT 1 2 3 4 1 Optimal Triangulation: Triangle 1: (0,1,2) 1 * 2 * 3 = 6 Triangle 2: (0,2,3) 1 * 3 * 4 = 12 OUTPUT 18 OK - Minimum Score Key Insight: The DP approach fixes two vertices (i and j) as the base of a triangle, then tries all possible third vertices (k). This divides the polygon into smaller subproblems. The minimum of dp[i][k] + dp[k][j] + values[i]*values[k]*values[j] gives the optimal triangulation. Time: O(n^3), Space: O(n^2). Vertex with value 1 minimizes products when included. TutorialsPoint - Minimum Score Triangulation of Polygon | DP Approach
Asked in
Google 25 Amazon 20 Microsoft 15
28.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen