Skip to content

Commit fe6be0e

Browse files
authored
Merge pull request #1326 from 0xff-dev/1039
Add solution and test-cases for problem 1039
2 parents 35db10b + fb1e0b0 commit fe6be0e

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
# [1039.Minimum Score Triangulation of Polygon][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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 `ith` vertex in **clockwise order**.
5+
6+
**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.
7+
8+
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.
9+
10+
Return the minimum possible score that you can achieve with some **triangulation** of the polygon.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: values = [1,2,3]
16+
17+
Output: 6
18+
19+
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
24+
```
25+
Input: values = [3,7,4,5]
26+
27+
Output: 144
28+
29+
Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144.
30+
The minimum score is 144.
31+
```
32+
33+
**Example 3:**
1934

20-
### 思路1
21-
> ...
22-
Minimum Score Triangulation of Polygon
23-
```go
2435
```
36+
Input: values = [1,3,1,4,1,5]
37+
38+
Output: 13
2539
40+
Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13.
41+
```
2642

2743
## 结语
2844

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(values []int) int {
4+
const maxn = (1 << 31) - 1
5+
6+
n := len(values)
7+
dp := make([][]int, n)
8+
for i := range n {
9+
dp[i] = make([]int, n)
10+
}
11+
12+
var cost int
13+
for l := 2; l < n; l++ {
14+
for i := range n - l {
15+
j := i + l
16+
dp[i][j] = maxn
17+
for k := i + 1; k < j; k++ {
18+
cost = dp[i][k] + dp[k][j] + values[i]*values[k]*values[j]
19+
dp[i][j] = min(dp[i][j], cost)
20+
}
21+
}
22+
}
23+
24+
return dp[0][n-1]
525
}

leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 3}, 6},
17+
{"TestCase2", []int{3, 7, 4, 5}, 144},
18+
{"TestCase3", []int{1, 3, 1, 4, 1, 5}, 13},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)