Skip to content

Commit b6f9f10

Browse files
committed
+ problem 1039
1 parent 42445ec commit b6f9f10

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 1039. Minimum Score Triangulation of Polygon
2+
You have a convex `n`-sided polygon where each vertex has an integer value. You are given an integer array `values` where `values[i]` is the value of the <code>i<sup>th</sup></code> vertex in **clockwise order**.
3+
4+
**Polygon triangulation** is a process where you divide a polygon into a set of triangles and the vertices of each triangle must also be vertices of the original polygon. Note that no other shapes other than triangles are allowed in the division. This process will result in `n - 2` triangles.
5+
6+
You will **triangulate** the polygon. For each triangle, the *weight* of that triangle is the product of the values at its vertices. The total score of the triangulation is the sum of these *weights* over all `n - 2` triangles.
7+
8+
Return the *minimum possible score* that you can achieve with some **triangulation** of the polygon.
9+
10+
#### Example 1:
11+
<pre>
12+
<strong>Input:</strong> values = [1,2,3]
13+
<strong>Output:</strong> 6
14+
<strong>Explanation:</strong> The polygon is already triangulated, and the score of the only triangle is 6.
15+
</pre>
16+
17+
#### Example 2:
18+
<pre>
19+
<strong>Input:</strong> values = [3,7,4,5]
20+
<strong>Output:</strong> 144
21+
<strong>Explanation:</strong> There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
22+
The minimum score is 144.
23+
</pre>
24+
25+
#### Example 3:
26+
<pre>
27+
<strong>Input:</strong> values = [1,3,1,4,1,5]
28+
<strong>Output:</strong> 13
29+
<strong>Explanation:</strong> The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
30+
</pre>
31+
32+
#### Constraints:
33+
* `n == values.length`
34+
* `3 <= n <= 50`
35+
* `1 <= values[i] <= 100`
36+
37+
## Solutions (Python)
38+
39+
### 1. Solution
40+
```Python
41+
from functools import cache
42+
43+
44+
class Solution:
45+
def minScoreTriangulation(self, values: List[int]) -> int:
46+
@cache
47+
def subMinScore(i: int, j: int) -> int:
48+
return min((values[i] * values[j] * values[k] + subMinScore(i, k) + subMinScore(k, j) for k in range(i + 1, j)), default=0)
49+
50+
return subMinScore(0, len(values) - 1)
51+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 1039. 多边形三角剖分的最低得分
2+
你有一个凸的 `n` 边形,其每个顶点都有一个整数值。给定一个整数数组 `values` ,其中 `values[i]` 是第 `i` 个顶点的值(即 **顺时针顺序** )。
3+
4+
假设将多边形 **剖分**`n - 2` 个三角形。对于每个三角形,该三角形的值是顶点标记的**乘积**,三角剖分的分数是进行三角剖分后所有 `n - 2` 个三角形的值之和。
5+
6+
返回 *多边形进行三角剖分后可以得到的最低分*
7+
8+
#### 示例 1:
9+
![](https://assets.leetcode.com/uploads/2021/02/25/shape1.jpg)
10+
<pre>
11+
<strong>输入:</strong> values = [1,2,3]
12+
<strong>输出:</strong> 6
13+
<strong>解释:</strong> 多边形已经三角化,唯一三角形的分数为 6。
14+
</pre>
15+
16+
#### 示例 2:
17+
![](https://assets.leetcode.com/uploads/2021/02/25/shape2.jpg)
18+
<pre>
19+
<strong>输入:</strong> values = [3,7,4,5]
20+
<strong>输出:</strong> 144
21+
<strong>解释:</strong> 有两种三角剖分,可能得分分别为:3*7*5 + 4*5*7 = 245,或 3*4*5 + 3*4*7 = 144。最低分数为 144。
22+
</pre>
23+
24+
#### 示例 3:
25+
![](https://assets.leetcode.com/uploads/2021/02/25/shape3.jpg)
26+
<pre>
27+
<strong>输入:</strong> values = [1,3,1,4,1,5]
28+
<strong>输出:</strong> 13
29+
<strong>解释:</strong> 最低分数三角剖分的得分情况为 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13。
30+
</pre>
31+
32+
#### 提示:
33+
* `n == values.length`
34+
* `3 <= n <= 50`
35+
* `1 <= values[i] <= 100`
36+
37+
## 题解 (Python)
38+
39+
### 1. 题解
40+
```Python
41+
from functools import cache
42+
43+
44+
class Solution:
45+
def minScoreTriangulation(self, values: List[int]) -> int:
46+
@cache
47+
def subMinScore(i: int, j: int) -> int:
48+
return min((values[i] * values[j] * values[k] + subMinScore(i, k) + subMinScore(k, j) for k in range(i + 1, j)), default=0)
49+
50+
return subMinScore(0, len(values) - 1)
51+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from functools import cache
2+
3+
4+
class Solution:
5+
def minScoreTriangulation(self, values: List[int]) -> int:
6+
@cache
7+
def subMinScore(i: int, j: int) -> int:
8+
return min((values[i] * values[j] * values[k] + subMinScore(i, k) + subMinScore(k, j) for k in range(i + 1, j)), default=0)
9+
10+
return subMinScore(0, len(values) - 1)

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@
710710
[1035][1035l]|[Uncrossed Lines][1035] |![rs]
711711
[1037][1037l]|[Valid Boomerang][1037] |![rs]
712712
[1038][1038l]|[Binary Search Tree to Greater Sum Tree][1038] |![py]
713+
[1039][1039l]|[Minimum Score Triangulation of Polygon][1039] |![py]
713714
[1040][1040l]|[Moving Stones Until Consecutive II][1040] |![rs]
714715
[1041][1041l]|[Robot Bounded In Circle][1041] |![rs]
715716
[1042][1042l]|[Flower Planting With No Adjacent][1042] |![rs]
@@ -2300,6 +2301,7 @@
23002301
[1035]:Problemset/1035-Uncrossed%20Lines/README.md#1035-uncrossed-lines
23012302
[1037]:Problemset/1037-Valid%20Boomerang/README.md#1037-valid-boomerang
23022303
[1038]:Problemset/1038-Binary%20Search%20Tree%20to%20Greater%20Sum%20Tree/README.md#1038-binary-search-tree-to-greater-sum-tree
2304+
[1039]:Problemset/1039-Minimum%20Score%20Triangulation%20of%20Polygon/README.md#1039-minimum-score-triangulation-of-polygon
23032305
[1040]:Problemset/1040-Moving%20Stones%20Until%20Consecutive%20II/README.md#1040-moving-stones-until-consecutive-ii
23042306
[1041]:Problemset/1041-Robot%20Bounded%20In%20Circle/README.md#1041-robot-bounded-in-circle
23052307
[1042]:Problemset/1042-Flower%20Planting%20With%20No%20Adjacent/README.md#1042-flower-planting-with-no-adjacent
@@ -3884,6 +3886,7 @@
38843886
[1035l]:https://leetcode.com/problems/uncrossed-lines/
38853887
[1037l]:https://leetcode.com/problems/valid-boomerang/
38863888
[1038l]:https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/
3889+
[1039l]:https://leetcode.com/problems/minimum-score-triangulation-of-polygon/
38873890
[1040l]:https://leetcode.com/problems/moving-stones-until-consecutive-ii/
38883891
[1041l]:https://leetcode.com/problems/robot-bounded-in-circle/
38893892
[1042l]:https://leetcode.com/problems/flower-planting-with-no-adjacent/

README_CN.md

+3
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@
710710
[1035][1035l]|[不相交的线][1035] |![rs]
711711
[1037][1037l]|[有效的回旋镖][1037] |![rs]
712712
[1038][1038l]|[从二叉搜索树到更大和树][1038] |![py]
713+
[1039][1039l]|[多边形三角剖分的最低得分][1039] |![py]
713714
[1040][1040l]|[移动石子直到连续 II][1040] |![rs]
714715
[1041][1041l]|[困于环中的机器人][1041] |![rs]
715716
[1042][1042l]|[不邻接植花][1042] |![rs]
@@ -2300,6 +2301,7 @@
23002301
[1035]:Problemset/1035-Uncrossed%20Lines/README_CN.md#1035-不相交的线
23012302
[1037]:Problemset/1037-Valid%20Boomerang/README_CN.md#1037-有效的回旋镖
23022303
[1038]:Problemset/1038-Binary%20Search%20Tree%20to%20Greater%20Sum%20Tree/README_CN.md#1038-从二叉搜索树到更大和树
2304+
[1039]:Problemset/1039-Minimum%20Score%20Triangulation%20of%20Polygon/README_CN.md#1039-多边形三角剖分的最低得分
23032305
[1040]:Problemset/1040-Moving%20Stones%20Until%20Consecutive%20II/README_CN.md#1040-移动石子直到连续-ii
23042306
[1041]:Problemset/1041-Robot%20Bounded%20In%20Circle/README_CN.md#1041-困于环中的机器人
23052307
[1042]:Problemset/1042-Flower%20Planting%20With%20No%20Adjacent/README_CN.md#1042-不邻接植花
@@ -3884,6 +3886,7 @@
38843886
[1035l]:https://leetcode.cn/problems/uncrossed-lines/
38853887
[1037l]:https://leetcode.cn/problems/valid-boomerang/
38863888
[1038l]:https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/
3889+
[1039l]:https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/
38873890
[1040l]:https://leetcode.cn/problems/moving-stones-until-consecutive-ii/
38883891
[1041l]:https://leetcode.cn/problems/robot-bounded-in-circle/
38893892
[1042l]:https://leetcode.cn/problems/flower-planting-with-no-adjacent/

0 commit comments

Comments
 (0)