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 ith
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.
You will triangulate the polygon. 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.
Example 1:
Input: values = [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
Example 2:
Input: values = [3,7,4,5]
Output: 144
Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
The minimum score is 144.
Example 3:
Input: values = [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
Constraints:
n == values.length
3 <= n <= 50
1 <= values[i] <= 100
This problem asks to find the minimum score achievable by triangulating a convex polygon. The score is the sum of the products of the values at the vertices of each triangle in the triangulation.
The problem can be efficiently solved using dynamic programming. We can define a subproblem dp[i][j]
as the minimum score for triangulating the polygon formed by vertices i
, i+1
, ..., j
.
The base case is when there are only two vertices (i.e., j - i == 1
), which results in a score of 0 since no triangulation is possible.
For j - i > 1
, we can consider all possible ways to triangulate the polygon by selecting a vertex k
between i
and j
as the third vertex of a triangle. This triangle will have a weight of values[i] * values[k] * values[j]
. The remaining polygon segments can be triangulated recursively, and their minimum scores are given by dp[i][k]
and dp[k][j]
. Therefore, the minimum score for the polygon [i, j]
can be expressed as:
dp[i][j] = min(dp[i][k] + dp[k][j] + values[i] * values[k] * values[j])
for all k
between i
and j
.
Time Complexity: The dynamic programming solution has three nested loops, each iterating up to n
times (where n
is the number of vertices). Therefore, the time complexity is O(n³).
Space Complexity: The solution uses a 2D DP table of size n x n
to store the minimum scores. Therefore, the space complexity is O(n²).
This implementation uses a bottom-up dynamic programming approach.
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): # Iterate through polygon lengths
for i in range(n - length + 1):
j = i + length - 1
dp[i][j] = float('inf') # Initialize with infinity
for k in range(i + 1, j): # Try all possible k
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i] * values[k] * values[j])
return dp[0][n - 1]
The code iterates through possible polygon lengths from 3 to n
, then iterates through possible starting vertices i
, and for each pair (i, j)
it explores all possible k
to find the minimum score. The result is stored in dp[0][n-1]
.
Other languages (Java, C++, Go, TypeScript) will have very similar implementations, with only syntactic differences reflecting the language features. The core algorithm remains the same, utilizing dynamic programming to solve the problem efficiently.