class Solution:
def minScoreTriangulation(self, values):
n = len(values)
dp = [[0] * n for _ in range(n)]
# dp[i][j]: min score to triangulate polygon from i to j
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):
cost = dp[i][k] + dp[k][j] + values[i] * values[k] * values[j]
dp[i][j] = min(dp[i][j], cost)
return dp[0][n - 1]
class Solution {
public:
int minScoreTriangulation(vector<int>& values) {
int n = values.size();
vector<vector<int>> dp(n, vector<int>(n, 0));
for (int length = 3; length <= n; ++length) {
for (int i = 0; i <= n - length; ++i) {
int j = i + length - 1;
dp[i][j] = INT_MAX;
for (int k = i + 1; k < j; ++k) {
int cost = dp[i][k] + dp[k][j] + values[i] * values[k] * values[j];
dp[i][j] = min(dp[i][j], cost);
}
}
}
return dp[0][n - 1];
}
};
class Solution {
public int minScoreTriangulation(int[] values) {
int n = values.length;
int[][] dp = new int[n][n];
for (int len = 3; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int j = i + len - 1;
dp[i][j] = Integer.MAX_VALUE;
for (int k = i + 1; k < j; k++) {
int cost = dp[i][k] + dp[k][j] + values[i] * values[k] * values[j];
dp[i][j] = Math.min(dp[i][j], cost);
}
}
}
return dp[0][n - 1];
}
}
var minScoreTriangulation = function(values) {
const n = values.length;
const dp = Array.from({length: n}, () => Array(n).fill(0));
for (let len = 3; len <= n; len++) {
for (let i = 0; i <= n - len; i++) {
let j = i + len - 1;
dp[i][j] = Infinity;
for (let k = i + 1; k < j; k++) {
let cost = dp[i][k] + dp[k][j] + values[i] * values[k] * values[j];
dp[i][j] = Math.min(dp[i][j], cost);
}
}
}
return dp[0][n - 1];
};
values
of length n
representing the values of vertices of a convex polygon in order, you must triangulate the polygon. Each triangle formed during triangulation has a score equal to the product of its three vertices' values. The total score of a triangulation is the sum of the scores of all triangles formed. Your task is to find the minimum possible total score for a triangulation of the entire polygon.
dp
where dp[i][j]
represents the minimum triangulation score for the sub-polygon from vertex i
to vertex j
.
j - i < 2
), it cannot form a triangle, so the score is 0.
k
between i
and j
(i < k < j
), the cost to form a triangle with vertices i
, k
, and j
is values[i] * values[k] * values[j]
. The total cost is this triangle's score plus the minimum scores for triangulating the sub-polygons (i, k)
and (k, j)
:
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i] * values[k] * values[j])
(i, j)
, try all possible k
between i
and j
to find the minimum score.
dp[0][n-1]
, which represents the minimum triangulation score for the entire polygon.
values = [1, 3, 1, 4, 1, 5]
.
dp[i][j] = 0
for all j - i < 2
.
i=0, j=3
(vertices 1, 3, 1, 4), we try k=1
and k=2
. For each, we compute:
k=1
: dp[0][1] + dp[1][3] + 1*3*4
k=2
: dp[0][2] + dp[2][3] + 1*1*4
dp[0][5]
, which gives the minimum score for the entire polygon.
n
.
n x n
table. For each pair (i, j)
, we try up to O(n)
possible k
values. There are O(n^2) pairs, so the total time complexity is O(n^3).