Skip to content

Commit 03011d8

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

File tree

8 files changed

+254
-10
lines changed

8 files changed

+254
-10
lines changed

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 题解(已完成 795 道)
1+
# LeetCode 题解(已完成 797 道)
22

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -254,6 +254,7 @@
254254
| 0370 | [区间加法](https://leetcode.cn/problems/range-addition/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0370.%20%E5%8C%BA%E9%97%B4%E5%8A%A0%E6%B3%95.md) | 数组、前缀和 | 中等 |
255255
| 0371 | [两整数之和](https://leetcode.cn/problems/sum-of-two-integers/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0371.%20%E4%B8%A4%E6%95%B4%E6%95%B0%E4%B9%8B%E5%92%8C.md) | 位运算、数学 | 中等 |
256256
| 0374 | [猜数字大小](https://leetcode.cn/problems/guess-number-higher-or-lower/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0374.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F.md) | 二分查找、交互 | 简单 |
257+
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0375.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F%20II.md) | 数学、动态规划、博弈 | 中等 |
257258
| 0376 | [摆动序列](https://leetcode.cn/problems/wiggle-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0376.%20%E6%91%86%E5%8A%A8%E5%BA%8F%E5%88%97.md) | 贪心、数组、动态规划 | 中等 |
258259
| 0377 | [组合总和 Ⅳ](https://leetcode.cn/problems/combination-sum-iv/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0377.%20%E7%BB%84%E5%90%88%E6%80%BB%E5%92%8C%20%E2%85%A3.md) | 数组、动态规划 | 中等 |
259260
| 0378 | [有序矩阵中第 K 小的元素](https://leetcode.cn/problems/kth-smallest-element-in-a-sorted-matrix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0378.%20%E6%9C%89%E5%BA%8F%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%AC%AC%20K%20%E5%B0%8F%E7%9A%84%E5%85%83%E7%B4%A0.md) | 数组、二分查找、矩阵、排序、堆(优先队列) | 中等 |
@@ -329,6 +330,7 @@
329330
| 0539 | [最小时间差](https://leetcode.cn/problems/minimum-time-difference/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0539.%20%E6%9C%80%E5%B0%8F%E6%97%B6%E9%97%B4%E5%B7%AE.md) | 数组、数学、字符串、排序 | 中等 |
330331
| 0542 | [01 矩阵](https://leetcode.cn/problems/01-matrix/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0542.%2001%20%E7%9F%A9%E9%98%B5.md) | 广度优先搜索、数组、动态规划、矩阵 | 中等 |
331332
| 0543 | [二叉树的直径](https://leetcode.cn/problems/diameter-of-binary-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0543.%20%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%9B%B4%E5%BE%84.md) | 树、深度优先搜索、二叉树 | 简单 |
333+
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0546.%20%E7%A7%BB%E9%99%A4%E7%9B%92%E5%AD%90.md) | 记忆化搜索、数组、动态规划 | 困难 |
332334
| 0547 | [省份数量](https://leetcode.cn/problems/number-of-provinces/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0547.%20%E7%9C%81%E4%BB%BD%E6%95%B0%E9%87%8F.md) | 深度优先搜索、广度优先搜索、并查集、图 | 中等 |
333335
| 0557 | [反转字符串中的单词 III](https://leetcode.cn/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0557.%20%E5%8F%8D%E8%BD%AC%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E7%9A%84%E5%8D%95%E8%AF%8D%20III.md) | 双指针、字符串 | 简单 |
334336
| 0560 | [和为 K 的子数组](https://leetcode.cn/problems/subarray-sum-equals-k/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0560.%20%E5%92%8C%E4%B8%BA%20K%20%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、哈希表、前缀和 | 中等 |

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@
837837
| 题号 | 标题 | 题解 | 标签 | 难度 |
838838
| :------ | :------ | :------ | :------ | :------ |
839839
| 1137 | [第 N 个泰波那契数](https://leetcode.cn/problems/n-th-tribonacci-number/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1137.%20%E7%AC%AC%20N%20%E4%B8%AA%E6%B3%B0%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.md) | 记忆化搜索、数学、动态规划 | 简单 |
840-
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
840+
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0375.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F%20II.md) | 数学、动态规划、博弈 | 中等 |
841841
| 0494 | [目标和](https://leetcode.cn/problems/target-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0494.%20%E7%9B%AE%E6%A0%87%E5%92%8C.md) | 数组、动态规划、回溯 | 中等 |
842842
| 0576 | [出界的路径数](https://leetcode.cn/problems/out-of-boundary-paths/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0576.%20%E5%87%BA%E7%95%8C%E7%9A%84%E8%B7%AF%E5%BE%84%E6%95%B0.md) | 动态规划 | 中等 |
843843
| 0087 | [扰乱字符串](https://leetcode.cn/problems/scramble-string/) | | 字符串、动态规划 | 困难 |
@@ -987,8 +987,8 @@
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) | 数组、动态规划、排序 | 困难 |
988988
| 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) | 字符串、动态规划 | 困难 |
989989
| 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) | 数组、动态规划 | 中等 |
990-
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | | 记忆化搜索、数组、动态规划 | 困难 |
991-
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
990+
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0546.%20%E7%A7%BB%E9%99%A4%E7%9B%92%E5%AD%90.md) | 记忆化搜索、数组、动态规划 | 困难 |
991+
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0375.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F%20II.md) | 数学、动态规划、博弈 | 中等 |
992992
| 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) | 栈、贪心、字符串、动态规划 | 中等 |
993993
| 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) | 字符串、动态规划 | 中等 |
994994
| 0516 | [最长回文子序列](https://leetcode.cn/problems/longest-palindromic-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0516.%20%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E5%BA%8F%E5%88%97.md) | 字符串、动态规划 | 中等 |

Contents/10.Dynamic-Programming/02.Memoization/02.Memoization-List.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
55
| 1137 | [第 N 个泰波那契数](https://leetcode.cn/problems/n-th-tribonacci-number/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1137.%20%E7%AC%AC%20N%20%E4%B8%AA%E6%B3%B0%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.md) | 记忆化搜索、数学、动态规划 | 简单 |
6-
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
6+
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0375.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F%20II.md) | 数学、动态规划、博弈 | 中等 |
77
| 0494 | [目标和](https://leetcode.cn/problems/target-sum/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0494.%20%E7%9B%AE%E6%A0%87%E5%92%8C.md) | 数组、动态规划、回溯 | 中等 |
88
| 0576 | [出界的路径数](https://leetcode.cn/problems/out-of-boundary-paths/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0576.%20%E5%87%BA%E7%95%8C%E7%9A%84%E8%B7%AF%E5%BE%84%E6%95%B0.md) | 动态规划 | 中等 |
99
| 0087 | [扰乱字符串](https://leetcode.cn/problems/scramble-string/) | | 字符串、动态规划 | 困难 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
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) | 数组、动态规划、排序 | 困难 |
1010
| 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) | 字符串、动态规划 | 困难 |
1111
| 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) | 数组、动态规划 | 中等 |
12-
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | | 记忆化搜索、数组、动态规划 | 困难 |
13-
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | | 数学、动态规划、博弈 | 中等 |
12+
| 0546 | [移除盒子](https://leetcode.cn/problems/remove-boxes/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0546.%20%E7%A7%BB%E9%99%A4%E7%9B%92%E5%AD%90.md) | 记忆化搜索、数组、动态规划 | 困难 |
13+
| 0375 | [猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0375.%20%E7%8C%9C%E6%95%B0%E5%AD%97%E5%A4%A7%E5%B0%8F%20II.md) | 数学、动态规划、博弈 | 中等 |
1414
| 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) | 栈、贪心、字符串、动态规划 | 中等 |
1515
| 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) | 字符串、动态规划 | 中等 |
1616
| 0516 | [最长回文子序列](https://leetcode.cn/problems/longest-palindromic-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0516.%20%E6%9C%80%E9%95%BF%E5%9B%9E%E6%96%87%E5%AD%90%E5%BA%8F%E5%88%97.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 题解(已完成 795 道)](./Contents/00.Introduction/04.Solutions-List.md)
262+
## [12. LeetCode 题解(已完成 797 道)](./Contents/00.Introduction/04.Solutions-List.md)

Solutions/0375. 猜数字大小 II.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# [0375. 猜数字大小 II](https://leetcode.cn/problems/guess-number-higher-or-lower-ii/)
2+
3+
- 标签:数学、动态规划、博弈
4+
- 难度:中等
5+
6+
## 题目大意
7+
8+
**描述**:现在两个人来玩一个猜数游戏,游戏规则如下:
9+
10+
1. 对方从 $1 \sim n$ 中选择一个数字。
11+
2. 我们来猜对方选了哪个数字。
12+
3. 如果我们猜到了正确数字,就会赢得游戏。
13+
4. 如果我们猜错了,那么对方就会告诉我们,所选的数字比我们猜的数字更大或者更小,并且需要我们继续猜数。
14+
5. 每当我们猜了数字 $x$ 并且猜错了的时候,我们需要支付金额为 $x$ 的现金。如果我们花光了钱,就会输掉游戏。
15+
16+
现在给定一个特定数字 $n$。
17+
18+
**要求**:返回能够确保我们获胜的最小现金数(不管对方选择哪个数字)。
19+
20+
**说明**
21+
22+
- $1 \le n \le 200$。
23+
24+
**示例**
25+
26+
- 示例 1:
27+
28+
![](https://assets.leetcode.com/uploads/2020/09/10/graph.png)
29+
30+
```Python
31+
输入:n = 10
32+
输出:16
33+
解释:制胜策略如下:
34+
- 数字范围是 [1,10]。你先猜测数字为 7
35+
- 如果这是我选中的数字,你的总费用为 $0。否则,你需要支付 $7
36+
- 如果我的数字更大,则下一步需要猜测的数字范围是 [8, 10] 。你可以猜测数字为 9
37+
- 如果这是我选中的数字,你的总费用为 $7。否则,你需要支付 $9
38+
- 如果我的数字更大,那么这个数字一定是 10。你猜测数字为 10 并赢得游戏,总费用为 $7 + $9 = $16
39+
- 如果我的数字更小,那么这个数字一定是 8。你猜测数字为 8 并赢得游戏,总费用为 $7 + $9 = $16
40+
- 如果我的数字更小,则下一步需要猜测的数字范围是 [1, 6]。你可以猜测数字为 3
41+
- 如果这是我选中的数字,你的总费用为 $7。否则,你需要支付 $3
42+
- 如果我的数字更大,则下一步需要猜测的数字范围是 [4, 6]。你可以猜测数字为 5
43+
- 如果这是我选中的数字,你的总费用为 $7 + $3 = $10 。否则,你需要支付 $5
44+
- 如果我的数字更大,那么这个数字一定是 6。你猜测数字为 6 并赢得游戏,总费用为 $7 + $3 + $5 = $15
45+
- 如果我的数字更小,那么这个数字一定是 4。你猜测数字为 4 并赢得游戏,总费用为 $7 + $3 + $5 = $15
46+
- 如果我的数字更小,则下一步需要猜测的数字范围是 [1, 2]。你可以猜测数字为 1
47+
- 如果这是我选中的数字,你的总费用为 $7 + $3 = $10。否则,你需要支付 $1
48+
- 如果我的数字更大,那么这个数字一定是 2。你猜测数字为 2 并赢得游戏,总费用为 $7 + $3 + $1 = $11
49+
在最糟糕的情况下,你需要支付 $16。因此,你只需要 $16 就可以确保自己赢得游戏。
50+
```
51+
52+
- 示例 2:
53+
54+
```Python
55+
输入:n = 2
56+
输出:1
57+
解释:有两个可能的数字 12
58+
- 你可以先猜 1
59+
- 如果这是我选中的数字,你的总费用为 $0 。否则,你需要支付 $1
60+
- 如果我的数字更大,那么这个数字一定是 2 。你猜测数字为 2 并赢得游戏,总费用为 $1
61+
最糟糕的情况下,你需要支付 $1
62+
```
63+
64+
## 解题思路
65+
66+
### 思路 1:动态规划
67+
68+
直觉上这道题应该通过二分查找来求解,但实际上并不能通过二分查找来求解。
69+
70+
因为我们可以通过二分查找方法,能够找到猜中的最小次数,但这个猜中的最小次数所对应的支付金额,并不是最小现金数。
71+
72+
也就是说,通过二分查找的策略,并不能找到确保我们获胜的最小现金数。所以我们需要转换思路。
73+
74+
我们可以用递归的方式来思考。
75+
76+
对于 $1 \sim n$ 中每一个数 $x$:
77+
78+
1. 如果 $x$ 恰好是正确数字,则获胜,付出的现金数为 $0$。
79+
2. 如果 $x$ 不是正确数字,则付出现金数为 $x$,同时我们得知,正确数字比 $x$ 更大还是更小。
80+
1. 如果正确数字比 $x$ 更小,我们只需要求出 $1 \sim x - 1$ 中能够获胜的最小现金数,再加上 $x$ 就是确保我们获胜的最小现金数。
81+
2. 如果正确数字比 $x$ 更大,我们只需要求出 $x + 1 \sim n$ 中能够获胜的最小现金数,再加上 $x$ 就是确保我们获胜的最小现金数。
82+
3. 因为正确数字可能比 $x$ 更小,也可能比 $x$ 更大。在考虑最坏情况下也能获胜,我们需要准备的最小现金应该为两种情况下的最小代价的最大值,再加上 $x$ 本身。
83+
84+
我们可以通过枚举 $x$,并求出所有情况下的最小值,即为确保我们获胜的最小现金数。
85+
86+
我们可以定义一个方法 $f(1)(n)$ 来表示 $1 \sim n$ 中能够获胜的最小现金数,则可以得到递推公式:$f(1)(n) = min_{x = 1}^{x = n} \lbrace max \lbrace f(1)(x - 1), f(x + 1)(n) \rbrace + x \rbrace)$。
87+
88+
将递推公式应用到 $i \sim j$ 中,可得:$f(i)(j) = min_{x = i}^{x = j} \lbrace max \lbrace f(i)(x - 1), f(x + 1)(j) \rbrace + x \rbrace)$
89+
90+
接下来我们就可以通过动态规划的方式解决这道题了。
91+
92+
###### 1. 划分阶段
93+
94+
按照区间长度进行阶段划分。
95+
96+
###### 2. 定义状态
97+
98+
定义状态 $dp[i][j]$ 表示为:数字 $i \sim j$ 中能够确保我们获胜的最小现金数。
99+
100+
###### 3. 状态转移方程
101+
102+
$dp[i][j] = min_{x = i}^{x = j} \lbrace max \lbrace dp[i][x - 1], dp[x + 1][j] \rbrace + x \rbrace)$
103+
104+
###### 4. 初始条件
105+
106+
- 默认数字 $i \sim j$ 中能够确保我们获胜的最小现金数为无穷大。
107+
- 当区间长度为 $1$ 时,区间中只有 $1$ 个数,肯定为正确数字,则付出最小现金数为 $0$,即 $dp[i][i] = 0$。
108+
109+
###### 5. 最终结果
110+
111+
根据我们之前定义的状态,$dp[i][j]$ 表示为:数字 $i \sim j$ 中能够确保我们获胜的最小现金数。所以最终结果为 $dp[1][n]$。
112+
113+
### 思路 1:代码
114+
115+
```Python
116+
class Solution:
117+
def getMoneyAmount(self, n: int) -> int:
118+
dp = [[0 for _ in range(n + 2)] for _ in range(n + 2)]
119+
for l in range(2, n + 1):
120+
for i in range(1, n + 1):
121+
j = i + l - 1
122+
if j > n:
123+
break
124+
dp[i][j] = float('inf')
125+
for k in range(i, j):
126+
dp[i][j] = min(dp[i][j], max(dp[i][k - 1] + k, dp[k + 1][j] + k))
127+
128+
return dp[1][n]
129+
130+
```
131+
132+
### 思路 1:复杂度分析
133+
134+
- **时间复杂度**:$O(n^3)$,其中 $n$ 为给定整数。
135+
- **空间复杂度**:$O(n^2)$。
136+

0 commit comments

Comments
 (0)