File tree 2 files changed +44
-1
lines changed
2 files changed +44
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,108 LeetCode solutions in JavaScript
1
+ # 1,109 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
846
846
1036|[ Escape a Large Maze] ( ./solutions/1036-escape-a-large-maze.js ) |Hard|
847
847
1037|[ Valid Boomerang] ( ./solutions/1037-valid-boomerang.js ) |Easy|
848
848
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|
849
850
1041|[ Robot Bounded In Circle] ( ./solutions/1041-robot-bounded-in-circle.js ) |Medium|
850
851
1047|[ Remove All Adjacent Duplicates In String] ( ./solutions/1047-remove-all-adjacent-duplicates-in-string.js ) |Easy|
851
852
1051|[ Height Checker] ( ./solutions/1051-height-checker.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments