1039. Minimum Score Triangulation of Polygon
Input: [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
Last updated
Input: [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
Last updated
Input: [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.Input: [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation has score 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.// Bottom-Up Dynamic Programming
int minScoreTriangulation(vector<int>& A) { // time: O(n^3); space: O(n^2)
int n = A.size();
vector<vector<int> > dp(n, vector<int>(n));
for (int j = 2; j < n; ++j) {
for (int i = j - 2; i >= 0; --i) {
dp[i][j] = INT_MAX;
for (int k = i + 1; k < j; ++k) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + A[i] * A[j] * A[k]);
}
}
}
return dp[0][n - 1];
}