diff --git a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/README.md b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/README.md index 5a448fbd9..057a40fc0 100644 --- a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/README.md +++ b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/README.md @@ -1,28 +1,44 @@ # [1039.Minimum Score Triangulation of Polygon][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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**. + +**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. + +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. + +Return the minimum possible score that you can achieve with some **triangulation** of the polygon. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: values = [1,2,3] + +Output: 6 + +Explanation: The polygon is already triangulated, and the score of the only triangle is 6. ``` -## 题意 -> ... +**Example 2:** -## 题解 +``` +Input: values = [3,7,4,5] + +Output: 144 + +Explanation: There are two triangulations, with possible scores: 3*7*5 + 4*5*7 = 245, or 3*4*5 + 3*4*7 = 144. +The minimum score is 144. +``` + +**Example 3:** -### 思路1 -> ... -Minimum Score Triangulation of Polygon -```go ``` +Input: values = [1,3,1,4,1,5] + +Output: 13 +Explanation: The minimum score triangulation is 1*1*3 + 1*1*4 + 1*1*5 + 1*1*1 = 13. +``` ## 结语 diff --git a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution.go b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution.go index d115ccf5e..627221b29 100644 --- a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution.go +++ b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(values []int) int { + const maxn = (1 << 31) - 1 + + n := len(values) + dp := make([][]int, n) + for i := range n { + dp[i] = make([]int, n) + } + + var cost int + for l := 2; l < n; l++ { + for i := range n - l { + j := i + l + dp[i][j] = maxn + for k := i + 1; k < j; k++ { + cost = dp[i][k] + dp[k][j] + values[i]*values[k]*values[j] + dp[i][j] = min(dp[i][j], cost) + } + } + } + + return dp[0][n-1] } diff --git a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution_test.go b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution_test.go index 14ff50eb4..b1381bccd 100644 --- a/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution_test.go +++ b/leetcode/1001-1100/1039.Minimum-Score-Triangulation-of-Polygon/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 3}, 6}, + {"TestCase2", []int{3, 7, 4, 5}, 144}, + {"TestCase3", []int{1, 3, 1, 4, 1, 5}, 13}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }