Skip to content

Commit 4ae63a3

Browse files
authored
Create 1039-minimum-score-triangulation-of-polygon.js
1 parent ef22908 commit 4ae63a3

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {number[]} A
3+
* @return {number}
4+
*/
5+
const minScoreTriangulation = function(A) {
6+
if(A.length <= 2) return 0
7+
if(A.length === 3) return A[0] * A[1] * A[2]
8+
return chk(A, A.length)
9+
};
10+
11+
function cost(points, i, j, k) {
12+
let p1 = points[i],
13+
p2 = points[j],
14+
p3 = points[k]
15+
return p1 * p2 * p3
16+
}
17+
18+
function chk(points, n) {
19+
if (n < 3) return 0
20+
21+
const table = Array.from({ length: n }, () => new Array(n).fill(0))
22+
23+
for (let gap = 0; gap < n; gap++) {
24+
for (let i = 0, j = gap; j < n; i++, j++) {
25+
if (j < i + 2) table[i][j] = 0
26+
else {
27+
table[i][j] = Number.MAX_VALUE
28+
for (let k = i + 1; k < j; k++) {
29+
let val = table[i][k] + table[k][j] + cost(points, i, j, k)
30+
if (table[i][j] > val) table[i][j] = val
31+
}
32+
}
33+
}
34+
}
35+
return table[0][n - 1]
36+
}

0 commit comments

Comments
 (0)