Skip to content

Commit 18d7125

Browse files
committed
Add solution #1039
1 parent 59121ca commit 18d7125

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,108 LeetCode solutions in JavaScript
1+
# 1,109 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -846,6 +846,7 @@
846846
1036|[Escape a Large Maze](./solutions/1036-escape-a-large-maze.js)|Hard|
847847
1037|[Valid Boomerang](./solutions/1037-valid-boomerang.js)|Easy|
848848
1038|[Binary Search Tree to Greater Sum Tree](./solutions/1038-binary-search-tree-to-greater-sum-tree.js)|Medium|
849+
1039|[Minimum Score Triangulation of Polygon](./solutions/1039-minimum-score-triangulation-of-polygon.js)|Medium|
849850
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|
850851
1047|[Remove All Adjacent Duplicates In String](./solutions/1047-remove-all-adjacent-duplicates-in-string.js)|Easy|
851852
1051|[Height Checker](./solutions/1051-height-checker.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* 1039. Minimum Score Triangulation of Polygon
3+
* https://leetcode.com/problems/minimum-score-triangulation-of-polygon/
4+
* Difficulty: Medium
5+
*
6+
* You have a convex n-sided polygon where each vertex has an integer value. You are given an
7+
* integer array values where values[i] is the value of the ith vertex in clockwise order.
8+
*
9+
* Polygon triangulation is a process where you divide a polygon into a set of triangles and
10+
* the vertices of each triangle must also be vertices of the original polygon. Note that no
11+
* other shapes other than triangles are allowed in the division. This process will result
12+
* in n - 2 triangles.
13+
*
14+
* You will triangulate the polygon. For each triangle, the weight of that triangle is the
15+
* product of the values at its vertices. The total score of the triangulation is the sum of
16+
* these weights over all n - 2 triangles.
17+
*
18+
* Return the minimum possible score that you can achieve with some triangulation of the polygon.
19+
*/
20+
21+
/**
22+
* @param {number[]} values
23+
* @return {number}
24+
*/
25+
var minScoreTriangulation = function(values) {
26+
const n = values.length;
27+
const dp = new Array(n).fill().map(() => new Array(n).fill(0));
28+
29+
for (let len = 2; len < n; len++) {
30+
for (let start = 0; start + len < n; start++) {
31+
const end = start + len;
32+
dp[start][end] = Infinity;
33+
34+
for (let mid = start + 1; mid < end; mid++) {
35+
const score = dp[start][mid] + dp[mid][end] + values[start] * values[mid] * values[end];
36+
dp[start][end] = Math.min(dp[start][end], score);
37+
}
38+
}
39+
}
40+
41+
return dp[0][n - 1];
42+
};

0 commit comments

Comments
 (0)