Skip to content

Commit 344b802

Browse files
committed
更新分类题解列表
1 parent ace9282 commit 344b802

File tree

7 files changed

+215
-9
lines changed

7 files changed

+215
-9
lines changed

Assets/Origins/Categories-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@
307307

308308
### [区间 DP 题目](../../Contents/10.Dynamic-Programming/05.Interval-DP/02.Interval-DP-List.md)
309309

310-
###### 0486. 预测赢家、0312. 戳气球、0877. 石子游戏、1000. 合并石头的最低成本、1547. 切棍子的最小成本、0664. 奇怪的打印机、1039. 多边形三角剖分的最低得分、0546. 移除盒子、1995. 统计特殊四元组、0375. 猜数字大小 II、0678. 有效的括号字符串、0005. 最长回文子串、0516. 最长回文子序列、0730. 统计不同回文子序列、2104. 子数组范围和
310+
###### 0486. 预测赢家、0312. 戳气球、0877. 石子游戏、1000. 合并石头的最低成本、1547. 切棍子的最小成本、0664. 奇怪的打印机、1039. 多边形三角剖分的最低得分、0546. 移除盒子、0375. 猜数字大小 II、0678. 有效的括号字符串、0005. 最长回文子串、0516. 最长回文子序列、0730. 统计不同回文子序列、2104. 子数组范围和
311311

312312
### [树形 DP 题目](../../Contents/10.Dynamic-Programming/06.Tree-DP/02.Tree-DP-List.md)
313313

Contents/00.Introduction/04.Solutions-List.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# LeetCode 题解(已完成 793 道)
1+
# LeetCode 题解(已完成 795 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -358,6 +358,7 @@
358358
| 0654 | [最大二叉树](https://leetcode.cn/problems/maximum-binary-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0654.%20%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91.md) | 栈、树、数组、分治、二叉树、单调栈 | 中等 |
359359
| 0658 | [找到 K 个最接近的元素](https://leetcode.cn/problems/find-k-closest-elements/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0658.%20%E6%89%BE%E5%88%B0%20K%20%E4%B8%AA%E6%9C%80%E6%8E%A5%E8%BF%91%E7%9A%84%E5%85%83%E7%B4%A0.md) | 数组、双指针、二分查找、排序、滑动窗口、堆(优先队列) | 中等 |
360360
| 0662 | [二叉树最大宽度](https://leetcode.cn/problems/maximum-width-of-binary-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0662.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E6%9C%80%E5%A4%A7%E5%AE%BD%E5%BA%A6.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 中等 |
361+
| 0664 | [奇怪的打印机](https://leetcode.cn/problems/strange-printer/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0664.%20%E5%A5%87%E6%80%AA%E7%9A%84%E6%89%93%E5%8D%B0%E6%9C%BA.md) | 字符串、动态规划 | 困难 |
361362
| 0665 | [非递减数列](https://leetcode.cn/problems/non-decreasing-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0665.%20%E9%9D%9E%E9%80%92%E5%87%8F%E6%95%B0%E5%88%97.md) | 数组 | 中等 |
362363
| 0669 | [修剪二叉搜索树](https://leetcode.cn/problems/trim-a-binary-search-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0669.%20%E4%BF%AE%E5%89%AA%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.md) | 树、深度优先搜索、二叉搜索树、二叉树 | 中等 |
363364
| 0673 | [最长递增子序列的个数](https://leetcode.cn/problems/number-of-longest-increasing-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0673.%20%E6%9C%80%E9%95%BF%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97%E7%9A%84%E4%B8%AA%E6%95%B0.md) | 树状数组、线段树、数组、动态规划 | 中等 |
@@ -494,6 +495,7 @@
494495
| 1035 | [不相交的线](https://leetcode.cn/problems/uncrossed-lines/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1035.%20%E4%B8%8D%E7%9B%B8%E4%BA%A4%E7%9A%84%E7%BA%BF.md) | 数组、动态规划 | 中等 |
495496
| 1037 | [有效的回旋镖](https://leetcode.cn/problems/valid-boomerang/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1037.%20%E6%9C%89%E6%95%88%E7%9A%84%E5%9B%9E%E6%97%8B%E9%95%96.md) | 几何、数组、数学 | 简单 |
496497
| 1038 | [从二叉搜索树到更大和树](https://leetcode.cn/problems/binary-search-tree-to-greater-sum-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1038.%20%E4%BB%8E%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E5%88%B0%E6%9B%B4%E5%A4%A7%E5%92%8C%E6%A0%91.md) | 树、深度优先搜索、二叉搜索树、二叉树 | 中等 |
498+
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1039.%20%E5%A4%9A%E8%BE%B9%E5%BD%A2%E4%B8%89%E8%A7%92%E5%89%96%E5%88%86%E7%9A%84%E6%9C%80%E4%BD%8E%E5%BE%97%E5%88%86.md) | 数组、动态规划 | 中等 |
497499
| 1047 | [删除字符串中的所有相邻重复项](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1047.%20%E5%88%A0%E9%99%A4%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E6%89%80%E6%9C%89%E7%9B%B8%E9%82%BB%E9%87%8D%E5%A4%8D%E9%A1%B9.md) | 栈、字符串 | 简单 |
498500
| 1049 | [最后一块石头的重量 II](https://leetcode.cn/problems/last-stone-weight-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1049.%20%E6%9C%80%E5%90%8E%E4%B8%80%E5%9D%97%E7%9F%B3%E5%A4%B4%E7%9A%84%E9%87%8D%E9%87%8F%20II.md) | 数组、动态规划 | 中等 |
499501
| 1052 | [爱生气的书店老板](https://leetcode.cn/problems/grumpy-bookstore-owner/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1052.%20%E7%88%B1%E7%94%9F%E6%B0%94%E7%9A%84%E4%B9%A6%E5%BA%97%E8%80%81%E6%9D%BF.md) | 数组、滑动窗口 | 中等 |

Contents/00.Introduction/05.Categories-List.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -985,10 +985,9 @@
985985
| 0877 | [石子游戏](https://leetcode.cn/problems/stone-game/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0877.%20%E7%9F%B3%E5%AD%90%E6%B8%B8%E6%88%8F.md) | 数组、数学、动态规划、博弈 | 中等 |
986986
| 1000 | [合并石头的最低成本](https://leetcode.cn/problems/minimum-cost-to-merge-stones/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1000.%20%E5%90%88%E5%B9%B6%E7%9F%B3%E5%A4%B4%E7%9A%84%E6%9C%80%E4%BD%8E%E6%88%90%E6%9C%AC.md) | 数组、动态规划、前缀和 | 困难 |
987987
| 1547 | [切棍子的最小成本](https://leetcode.cn/problems/minimum-cost-to-cut-a-stick/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1547.%20%E5%88%87%E6%A3%8D%E5%AD%90%E7%9A%84%E6%9C%80%E5%B0%8F%E6%88%90%E6%9C%AC.md) | 数组、动态规划、排序 | 困难 |
988-
| 0664 | [奇怪的打印机](https://leetcode.cn/problems/strange-printer/) | | 字符串、动态规划 | 困难 |
989-
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | | 数组、动态规划 | 中等 |
988+
| 0664 | [奇怪的打印机](https://leetcode.cn/problems/strange-printer/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0664.%20%E5%A5%87%E6%80%AA%E7%9A%84%E6%89%93%E5%8D%B0%E6%9C%BA.md) | 字符串、动态规划 | 困难 |
989+
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1039.%20%E5%A4%9A%E8%BE%B9%E5%BD%A2%E4%B8%89%E8%A7%92%E5%89%96%E5%88%86%E7%9A%84%E6%9C%80%E4%BD%8E%E5%BE%97%E5%88%86.md) | 数组、动态规划 | 中等 |
990990
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | | 记忆化搜索、数组、动态规划 | 困难 |
991-
| 1995 | [统计特殊四元组](https://leetcode.cn/problems/count-special-quadruplets/) | | 数组、枚举 | 简单 |
992991
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
993992
| 0678 | [有效的括号字符串](https://leetcode.cn/problems/valid-parenthesis-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0678.%20%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 栈、贪心、字符串、动态规划 | 中等 |
994993
| 0005 | [最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0005.%20%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.md) | 字符串、动态规划 | 中等 |

Contents/10.Dynamic-Programming/05.Interval-DP/02.Interval-DP-List.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77
| 0877 | [石子游戏](https://leetcode.cn/problems/stone-game/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0877.%20%E7%9F%B3%E5%AD%90%E6%B8%B8%E6%88%8F.md) | 数组、数学、动态规划、博弈 | 中等 |
88
| 1000 | [合并石头的最低成本](https://leetcode.cn/problems/minimum-cost-to-merge-stones/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1000.%20%E5%90%88%E5%B9%B6%E7%9F%B3%E5%A4%B4%E7%9A%84%E6%9C%80%E4%BD%8E%E6%88%90%E6%9C%AC.md) | 数组、动态规划、前缀和 | 困难 |
99
| 1547 | [切棍子的最小成本](https://leetcode.cn/problems/minimum-cost-to-cut-a-stick/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1547.%20%E5%88%87%E6%A3%8D%E5%AD%90%E7%9A%84%E6%9C%80%E5%B0%8F%E6%88%90%E6%9C%AC.md) | 数组、动态规划、排序 | 困难 |
10-
| 0664 | [奇怪的打印机](https://leetcode.cn/problems/strange-printer/) | | 字符串、动态规划 | 困难 |
11-
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | | 数组、动态规划 | 中等 |
10+
| 0664 | [奇怪的打印机](https://leetcode.cn/problems/strange-printer/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0664.%20%E5%A5%87%E6%80%AA%E7%9A%84%E6%89%93%E5%8D%B0%E6%9C%BA.md) | 字符串、动态规划 | 困难 |
11+
| 1039 | [多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1039.%20%E5%A4%9A%E8%BE%B9%E5%BD%A2%E4%B8%89%E8%A7%92%E5%89%96%E5%88%86%E7%9A%84%E6%9C%80%E4%BD%8E%E5%BE%97%E5%88%86.md) | 数组、动态规划 | 中等 |
1212
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | | 记忆化搜索、数组、动态规划 | 困难 |
13-
| 1995 | [统计特殊四元组](https://leetcode.cn/problems/count-special-quadruplets/) | | 数组、枚举 | 简单 |
1413
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
1514
| 0678 | [有效的括号字符串](https://leetcode.cn/problems/valid-parenthesis-string/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0678.%20%E6%9C%89%E6%95%88%E7%9A%84%E6%8B%AC%E5%8F%B7%E5%AD%97%E7%AC%A6%E4%B8%B2.md) | 栈、贪心、字符串、动态规划 | 中等 |
1615
| 0005 | [最长回文子串](https://leetcode.cn/problems/longest-palindromic-substring/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0005.%20%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E4%B8%B2.md) | 字符串、动态规划 | 中等 |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,4 @@
259259
- [动态规划优化题目](./Contents/10.Dynamic-Programming/11.DP-Optimization/04.DP-Optimization-List.md)
260260

261261
## 11. 附加内容
262-
## [12. LeetCode 题解(已完成 793 道)](./Contents/00.Introduction/04.Solutions-List.md)
262+
## [12. LeetCode 题解(已完成 795 道)](./Contents/00.Introduction/04.Solutions-List.md)

Solutions/0664. 奇怪的打印机.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# [0664. 奇怪的打印机](https://leetcode.cn/problems/strange-printer/)
2+
3+
- 标签:字符串、动态规划
4+
- 难度:困难
5+
6+
## 题目大意
7+
8+
**描述**:有一台奇怪的打印机,有以下两个功能:
9+
10+
1. 打印机每次只能打印由同一个字符组成的序列,比如:`"aaaa"``"bbb"`
11+
2. 每次可以从起始位置到结束的任意为止打印新字符,并且会覆盖掉原有字符。
12+
13+
现在给定一个字符串 $s$。
14+
15+
**要求**:计算这个打印机打印出字符串 $s$ 需要的最少打印次数。
16+
17+
**说明**
18+
19+
- $1 \le s.length \le 100$。
20+
- $s$ 由小写英文字母组成。
21+
22+
**示例**
23+
24+
- 示例 1:
25+
26+
```Python
27+
输入:s = "aaabbb"
28+
输出:2
29+
解释:首先打印 "aaa" 然后打印 "bbb"
30+
```
31+
32+
- 示例 2:
33+
34+
```Python
35+
输入:s = "aba"
36+
输出:2
37+
解释:首先打印 "aaa" 然后在第二个位置打印 "b" 覆盖掉原来的字符 'a'
38+
```
39+
40+
## 解题思路
41+
42+
对于字符串 $s$,我们可以先考虑区间 $[i, j]$ 上的子字符串需要的最少打印次数。
43+
44+
1. 如果区间 $[i, j]$ 内只有 $1$ 种字符,则最少打印次数为 $1$,即:$dp[i][i] = 1$。
45+
2. 如果区间 $[i, j]$ 内首尾字符相同,即 $s[i] == s[j]$,则我们在打印 $s[i]$ 的同时我们可以顺便打印 $s[j]$,这样我们可以忽略 $s[j]$,只考虑剩下区间 $[i, j - 1]$ 的打印情况,即:$dp[i][j] = dp[i][j - 1]$。
46+
3. 如果区间 $[i, j]$ 上首尾字符不同,即 $s[i] \ne s[j]$,则枚举分割点 $k$,将区间 $[i, j]$ 分为区间 $[i, k]$ 与区间 $[k + 1, j]$,使得 $dp[i][k] + dp[k + 1][j]$ 的值最小即为 $dp[i][j]$。
47+
48+
### 思路 1:动态规划
49+
50+
###### 1. 划分阶段
51+
52+
按照区间长度进行阶段划分。
53+
54+
###### 2. 定义状态
55+
56+
定义状态 $dp[i][j]$ 表示为:打印第 $i$ 个字符到第 $j$ 个字符需要的最少打印次数。
57+
58+
###### 3. 状态转移方程
59+
60+
1. 如果 $s[i] == s[j]$,则我们在打印 $s[i]$ 的同时我们可以顺便打印 $s[j]$,这样我们可以忽略 $s[j]$,只考虑剩下区间 $[i, j - 1]$ 的打印情况,即:$dp[i][j] = dp[i][j - 1]$。
61+
2. 如果 $s[i] \ne s[j]$,则枚举分割点 $k$,将区间 $[i, j]$ 分为区间 $[i, k]$ 与区间 $[k + 1, j]$,使得 $dp[i][k] + dp[k + 1][j]$ 的值最小即为 $dp[i][j]$,即:$dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])$。
62+
63+
###### 4. 初始条件
64+
65+
- 初始时,打印单个字符的最少打印次数为 $1$,即 $dp[i][i] = 1$。
66+
67+
###### 5. 最终结果
68+
69+
根据我们之前定义的状态,$dp[i][j]$ 表示为:打印第 $i$ 个字符到第 $j$ 个字符需要的最少打印次数。 所以最终结果为 $dp[0][size - 1]$。
70+
71+
### 思路 1:代码
72+
73+
```Python
74+
class Solution:
75+
def strangePrinter(self, s: str) -> int:
76+
size = len(s)
77+
dp = [[float('inf') for _ in range(size)] for _ in range(size)]
78+
for i in range(size):
79+
dp[i][i] = 1
80+
81+
for l in range(2, size + 1):
82+
for i in range(size):
83+
j = i + l - 1
84+
if j >= size:
85+
break
86+
if s[i] == s[j]:
87+
dp[i][j] = dp[i][j - 1]
88+
else:
89+
for k in range(i, j):
90+
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j])
91+
92+
return dp[0][size - 1]
93+
```
94+
95+
### 思路 1:复杂度分析
96+
97+
- **时间复杂度**:$O(n^3)$,其中 $n$ 为字符串 $s$ 的长度。
98+
- **空间复杂度**:$O(n^2)$。
99+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [1039. 多边形三角剖分的最低得分](https://leetcode.cn/problems/minimum-score-triangulation-of-polygon/)
2+
3+
- 标签:数组、动态规划
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
**描述**:有一个凸的 $n$ 边形,其每个顶点都有一个整数值。给定一个整数数组 $values$,其中 $values[i]$ 是第 $i$ 个顶点的值(即顺时针顺序)。
9+
10+
现在要将 $n$ 边形剖分为 $n - 2$ 个三角形,对于每个三角形,该三角形的值是顶点标记的乘积,$n$ 边形三角剖分的分数是进行三角剖分后所有 $n - 2$ 个三角形的值之和。
11+
12+
**要求**:返回多边形进行三角剖分可以得到的最低分。
13+
14+
**说明**
15+
16+
- $n == values.length$。
17+
- $3 \le n \le 50$。
18+
- $1 \le values[i] \le 100$。
19+
20+
**示例**
21+
22+
- 示例 1:
23+
24+
![](https://assets.leetcode.com/uploads/2021/02/25/shape1.jpg)
25+
26+
```Python
27+
输入:values = [1,2,3]
28+
输出:6
29+
解释:多边形已经三角化,唯一三角形的分数为 6
30+
```
31+
32+
- 示例 2:
33+
34+
![](https://assets.leetcode.com/uploads/2021/02/25/shape2.jpg)
35+
36+
```Python
37+
输入:values = [3,7,4,5]
38+
输出:144
39+
解释:有两种三角剖分,可能得分分别为:3*7*5 + 4*5*7 = 245,或 3*4*5 + 3*4*7 = 144。最低分数为 144
40+
```
41+
42+
## 解题思路
43+
44+
### 思路 1:动态规划
45+
46+
对于 $0 \sim n - 1$ 个顶点组成的凸多边形进行三角剖分,我们可以在 $[0, n - 1]$ 中任选 $1$ 个点 $k$,从而将凸多边形划分为:
47+
48+
1. 顶点 $0 \sim k$ 组成的凸多边形。
49+
2. 顶点 $0$、$k$、$n - 1$ 组成的三角形。
50+
3. 顶点 $k \sim n - 1$ 组成的凸多边形。
51+
52+
对于顶点 $0$、$k$、$n - 1$ 组成的三角形,我们可以直接计算对应的三角剖分分数为 $values[0] \times values[k] \times values[n - 1]$。
53+
54+
而对于顶点 $0 \sim k$ 组成的凸多边形和顶点 $k \sim n - 1$ 组成的凸多边形,我们可以利用递归或者动态规划的思想,定义一个 $dp[i][j]$ 用于计算顶点 $i$ 到顶点 $j$ 组成的多边形三角剖分的最小分数。
55+
56+
具体做法如下:
57+
58+
###### 1. 划分阶段
59+
60+
按照区间长度进行阶段划分。
61+
62+
###### 2. 定义状态
63+
64+
定义状态 $dp[i][j]$ 表示为:区间 $[i, j]$ 内三角剖分后的最小分数。
65+
66+
###### 3. 状态转移方程
67+
68+
对于区间 $[i, j]$,枚举分割点 $k$,最小分数为 $min(dp[i][k] + dp[k][j] + values[i] \times values[k] \times values[j])$,即:$dp[i][j] = min(dp[i][k] + dp[k][j] + values[i] \times values[k] \times values[j])$。
69+
70+
###### 4. 初始条件
71+
72+
- 默认情况下,所有区间 $[i, j]$ 的最小分数为无穷大。
73+
- 当区间 $[i, j]$ 长度小于 $3$ 时,无法进行三角剖分,其最小分数为 $0$。
74+
- 当区间 $[i, j]$ 长度等于 $3$ 时,其三角剖分的最小分数为 $values[i] * values[i + 1] * values[i + 2]$。
75+
76+
###### 5. 最终结果
77+
78+
根据我们之前定义的状态,$dp[i][j]$ 表示为:区间 $[i, j]$ 内三角剖分后的最小分数。。 所以最终结果为 $dp[0][size - 1]$。
79+
80+
### 思路 1:代码
81+
82+
```Python
83+
class Solution:
84+
def minScoreTriangulation(self, values: List[int]) -> int:
85+
size = len(values)
86+
dp = [[float('inf') for _ in range(size)] for _ in range(size)]
87+
for l in range(1, size + 1):
88+
for i in range(size):
89+
j = i + l - 1
90+
if j >= size:
91+
break
92+
if l < 3:
93+
dp[i][j] = 0
94+
elif l == 3:
95+
dp[i][j] = values[i] * values[i + 1] * values[i + 2]
96+
else:
97+
for k in range(i + 1, j):
98+
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + values[i] * values[j] * values[k])
99+
100+
return dp[0][size - 1]
101+
```
102+
103+
### 思路 1:复杂度分析
104+
105+
- **时间复杂度**
106+
- **空间复杂度**
107+

0 commit comments

Comments
 (0)