[LeetCode] Minimum Score Triangulation of Polygon

1039. Minimum Score Triangulation of Polygon

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 (i.e., clockwise order).

You will triangulate the polygon into n - 2 triangles. For each triangle, the value of that triangle is the product of the values of its vertices, and the total score of the triangulation is the sum of these values over all n - 2 triangles in the triangulation.

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
int dp[51][51];
const int INF = 987654321;
int helper(vector<int>& A, int s, int e) {
if(dp[s][e] != -1) return dp[s][e];
dp[s][e] = INF;
for(int i = s + 1; i < e; i++) {
dp[s][e] = min(dp[s][e],
helper(A,s,i) + helper(A,i,e) + A[s] * A[e] * A[i]
);
}
if(dp[s][e] == INF) dp[s][e] = 0;
return dp[s][e];
}
public:
int minScoreTriangulation(vector<int>& values) {
memset(dp, -1, sizeof dp);

return helper(values, 0, values.size() - 1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/12/PS/LeetCode/minimum-score-triangulation-of-polygon/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.