Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec60676

Browse files
committed
更新题解列表
1 parent ba19b32 commit ec60676

13 files changed

+447
-96
lines changed

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

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

33
| 题号 | 标题 | 题解 | 标签 | 难度 |
44
| :------ | :------ | :------ | :------ | :------ |
@@ -315,7 +315,7 @@
315315
| 0513 | [找树左下角的值](https://leetcode.cn/problems/find-bottom-left-tree-value/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0513.%20%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 中等 |
316316
| 0515 | [在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0515.%20%E5%9C%A8%E6%AF%8F%E4%B8%AA%E6%A0%91%E8%A1%8C%E4%B8%AD%E6%89%BE%E6%9C%80%E5%A4%A7%E5%80%BC.md) | 树、深度优先搜索、广度优先搜索、二叉树 | 中等 |
317317
| 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) | 字符串、动态规划 | 中等 |
318-
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-2/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
318+
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
319319
| 0525 | [连续数组](https://leetcode.cn/problems/contiguous-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0525.%20%E8%BF%9E%E7%BB%AD%E6%95%B0%E7%BB%84.md) | 数组、哈希表、前缀和 | 中等 |
320320
| 0530 | [二叉搜索树的最小绝对差](https://leetcode.cn/problems/minimum-absolute-difference-in-bst/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0530.%20%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E7%BB%9D%E5%AF%B9%E5%B7%AE.md) | 树、深度优先搜索、广度优先搜索、二叉搜索树、二叉树 | 简单 |
321321
| 0538 | [把二叉搜索树转换为累加树](https://leetcode.cn/problems/convert-bst-to-greater-tree/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0538.%20%E6%8A%8A%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E8%BD%AC%E6%8D%A2%E4%B8%BA%E7%B4%AF%E5%8A%A0%E6%A0%91.md) | 树、深度优先搜索、二叉搜索树、二叉树 | 中等 |
@@ -514,6 +514,7 @@
514514
| 1423 | [可获得的最大点数](https://leetcode.cn/problems/maximum-points-you-can-obtain-from-cards/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1423.%20%E5%8F%AF%E8%8E%B7%E5%BE%97%E7%9A%84%E6%9C%80%E5%A4%A7%E7%82%B9%E6%95%B0.md) | 数组、前缀和、滑动窗口 | 中等 |
515515
| 1438 | [绝对差不超过限制的最长连续子数组](https://leetcode.cn/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1438.%20%E7%BB%9D%E5%AF%B9%E5%B7%AE%E4%B8%8D%E8%B6%85%E8%BF%87%E9%99%90%E5%88%B6%E7%9A%84%E6%9C%80%E9%95%BF%E8%BF%9E%E7%BB%AD%E5%AD%90%E6%95%B0%E7%BB%84.md) | 队列、数组、有序集合、滑动窗口、单调队列、堆(优先队列) | 中等 |
516516
| 1446 | [连续字符](https://leetcode.cn/problems/consecutive-characters/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1446.%20%E8%BF%9E%E7%BB%AD%E5%AD%97%E7%AC%A6.md) | 字符串 | 简单 |
517+
| 1449 | [数位成本和为目标值的最大数字](https://leetcode.cn/problems/form-largest-integer-with-digits-that-add-up-to-target/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1449.%20%E6%95%B0%E4%BD%8D%E6%88%90%E6%9C%AC%E5%92%8C%E4%B8%BA%E7%9B%AE%E6%A0%87%E5%80%BC%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E5%AD%97.md) | 数组、动态规划 | 困难 |
517518
| 1450 | [在既定时间做作业的学生人数](https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1450.%20%E5%9C%A8%E6%97%A2%E5%AE%9A%E6%97%B6%E9%97%B4%E5%81%9A%E4%BD%9C%E4%B8%9A%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BA%BA%E6%95%B0.md) | 数组 | 简单 |
518519
| 1456 | [定长子串中元音的最大数目](https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1456.%20%E5%AE%9A%E9%95%BF%E5%AD%90%E4%B8%B2%E4%B8%AD%E5%85%83%E9%9F%B3%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E7%9B%AE.md) | 字符串、滑动窗口 | 中等 |
519520
| 1480 | [一维数组的动态和](https://leetcode.cn/problems/running-sum-of-1d-array/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1480.%20%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%9A%84%E5%8A%A8%E6%80%81%E5%92%8C.md) | 数组 | 简单 |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,11 @@
951951
| :------ | :------ | :------ | :------ | :------ |
952952
| 0279 | [完全平方数](https://leetcode.cn/problems/perfect-squares/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0279.%20%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0.md) | 广度优先搜索、数学、动态规划 | 中等 |
953953
| 0322 | [零钱兑换](https://leetcode.cn/problems/coin-change/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0322.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2.md) | 动态规划 | 中等 |
954-
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-2/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
954+
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
955955
| 0139 | [单词拆分](https://leetcode.cn/problems/word-break/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0139.%20%E5%8D%95%E8%AF%8D%E6%8B%86%E5%88%86.md) | 字典树、记忆化搜索、哈希表、字符串、动态规划 | 中等 |
956956
| 0377 | [组合总和 IV](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) | 数组、动态规划 | 中等 |
957957
| 0638 | 大礼包 | | | |
958-
| 1449 | 数位成本和为目标值的最大数字 | || |
958+
| 1449 | [数位成本和为目标值的最大数字](https://leetcode.cn/problems/form-largest-integer-with-digits-that-add-up-to-target/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1449.%20%E6%95%B0%E4%BD%8D%E6%88%90%E6%9C%AC%E5%92%8C%E4%B8%BA%E7%9B%AE%E6%A0%87%E5%80%BC%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E5%AD%97.md)| 数组、动态规划 | 困难 |
959959

960960
#### 多重背包问题
961961

‎Contents/00.Introduction/07.Interview-200-List.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@
472472
| 0509 | [斐波那契数](https://leetcode.cn/problems/fibonacci-number/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0509.%20%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0.md) | 数组 | 简单 |
473473
| 0121 | [买卖股票的最佳时机](https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0121.%20%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%9C%BA.md) | 数组、动态规划 | 简单 |
474474
| 0322 | [零钱兑换](https://leetcode.cn/problems/coin-change/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0322.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2.md) | 动态规划 | 中等 |
475-
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-2/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
475+
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
476476
| 0300 | [最长递增子序列](https://leetcode.cn/problems/longest-increasing-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0300.%20%E6%9C%80%E9%95%BF%E9%80%92%E5%A2%9E%E5%AD%90%E5%BA%8F%E5%88%97.md) | 二分查找、动态规划 | 中等 |
477477
| 1143 | [最长公共子序列](https://leetcode.cn/problems/longest-common-subsequence/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1143.%20%E6%9C%80%E9%95%BF%E5%85%AC%E5%85%B1%E5%AD%90%E5%BA%8F%E5%88%97.md) | 字符串、动态规划 | 中等 |
478478
| 0718 | [最长重复子数组](https://leetcode.cn/problems/maximum-length-of-repeated-subarray/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0718.%20%E6%9C%80%E9%95%BF%E9%87%8D%E5%A4%8D%E5%AD%90%E6%95%B0%E7%BB%84.md) | 数组、二分查找、动态规划、滑动窗口、哈希函数、滚动哈希 | 中等 |

‎Contents/10.Dynamic-Programming/04.Knapsack-Problem/06.Knapsack-Problem-List.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
| :------ | :------ | :------ | :------ | :------ |
1515
| 0279 | [完全平方数](https://leetcode.cn/problems/perfect-squares/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0279.%20%E5%AE%8C%E5%85%A8%E5%B9%B3%E6%96%B9%E6%95%B0.md) | 广度优先搜索、数学、动态规划 | 中等 |
1616
| 0322 | [零钱兑换](https://leetcode.cn/problems/coin-change/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0322.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2.md) | 动态规划 | 中等 |
17-
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-2/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
17+
| 0518 | [零钱兑换 II](https://leetcode.cn/problems/coin-change-ii/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0518.%20%E9%9B%B6%E9%92%B1%E5%85%91%E6%8D%A2%20II.md) | 数组、动态规划 | 中等 |
1818
| 0139 | [单词拆分](https://leetcode.cn/problems/word-break/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/0139.%20%E5%8D%95%E8%AF%8D%E6%8B%86%E5%88%86.md) | 字典树、记忆化搜索、哈希表、字符串、动态规划 | 中等 |
1919
| 0377 | [组合总和 IV](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) | 数组、动态规划 | 中等 |
2020
| 0638 | 大礼包 | | | |
21-
| 1449 | 数位成本和为目标值的最大数字 | || |
21+
| 1449 | [数位成本和为目标值的最大数字](https://leetcode.cn/problems/form-largest-integer-with-digits-that-add-up-to-target/) | [Python](https://github.com/itcharge/LeetCode-Py/blob/main/Solutions/1449.%20%E6%95%B0%E4%BD%8D%E6%88%90%E6%9C%AC%E5%92%8C%E4%B8%BA%E7%9B%AE%E6%A0%87%E5%80%BC%E7%9A%84%E6%9C%80%E5%A4%A7%E6%95%B0%E5%AD%97.md)| 数组、动态规划 | 困难 |
2222

2323
#### 多重背包问题
2424

‎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 题解(已完成 748 道)](./Contents/00.Introduction/04.Solutions-List.md)
262+
## [12. LeetCode 题解(已完成 749 道)](./Contents/00.Introduction/04.Solutions-List.md)

‎Solutions/0279. 完全平方数.md‎

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
## 题目大意
77

8-
**描述**:给定一个正整数 `n`。从中找到若干个完全平方数(比如 `1``4``1``16` ...),使得它们的和等于 `n`
8+
**描述**:给定一个正整数 $n$。从中找到若干个完全平方数(比如 1ドル、4、9、16...$),使得它们的和等于 $n$
99

10-
**要求**:返回和为 `n` 的完全平方数的最小数量。
10+
**要求**:返回和为 $n$ 的完全平方数的最小数量。
1111

1212
**说明**:
1313

@@ -33,7 +33,7 @@
3333

3434
## 解题思路
3535

36-
暴力枚举思路:对于小于 `n` 的完全平方数,直接暴力枚举所有可能的组合,并且找到平方数个数最小的一个。
36+
暴力枚举思路:对于小于 $n$ 的完全平方数,直接暴力枚举所有可能的组合,并且找到平方数个数最小的一个。
3737

3838
并且对于所有小于 $n$ 的完全平方数($k = 1, 4, 9, 16, ...$),存在公式:$ans(n) = min(ans(n - k) + 1),k = 1,4,9,16,...$
3939

@@ -45,22 +45,22 @@
4545

4646
我们可以转换一下思维。
4747

48-
1.`n` 作为根节点,构建一棵多叉数。
49-
2.`n` 节点出发,如果一个小于 `n` 的数刚好与 `n` 相差一个平方数,则以该数为值构造一个节点,与 `n` 相连。
48+
1.$n$ 作为根节点,构建一棵多叉数。
49+
2.$n$ 节点出发,如果一个小于 $n$ 的数刚好与 $n$ 相差一个平方数,则以该数为值构造一个节点,与 $n$ 相连。
5050

51-
那么求解和为 `n` 的完全平方数的最小数量就变成了求解这棵树从根节点 `n` 到节点 `0` 的最短路径,或者说树的最小深度。
51+
那么求解和为 $n$ 的完全平方数的最小数量就变成了求解这棵树从根节点 $n$ 到节点 0ドル$ 的最短路径,或者说树的最小深度。
5252

5353
这个过程可以通过广度优先搜索来做。
5454

5555
### 思路 1:广度优先搜索
5656

57-
1. 定义 `visited` 为标记访问节点的 set 集合变量,避免重复计算。定义 `queue` 为存放节点的队列。使用 `count` 表示为树的最小深度,也就是和为 `n` 的完全平方数的最小数量。
58-
2. 首先,我们将 `n` 标记为已访问,即 `visited.add(n)`。并将其加入队列 `queue` 中,即 `queue.append(n)`
59-
3.`count``1`,表示最小深度加 `1`。然后依次将队列中的节点值取出。
60-
4. 对于取出的节点值 `value`,遍历可能出现的平方数(即遍历 $[1, \sqrt{value} + 1]$ 中的数)。
57+
1. 定义 $visited$ 为标记访问节点的 set 集合变量,避免重复计算。定义 $queue$ 为存放节点的队列。使用 $count$ 表示为树的最小深度,也就是和为 $n$ 的完全平方数的最小数量。
58+
2. 首先,我们将 $n$ 标记为已访问,即 `visited.add(n)`。并将其加入队列 $queue$ 中,即 `queue.append(n)`
59+
3.$count$1ドル$,表示最小深度加 1ドル$。然后依次将队列中的节点值取出。
60+
4. 对于取出的节点值 $value$,遍历可能出现的平方数(即遍历 $[1, \sqrt{value} + 1]$ 中的数)。
6161
5. 每次从当前节点值减去一个平方数,并将减完的数加入队列。
62-
1. 如果此时的数等于 `0`,则满足题意,返回当前树的最小深度。
63-
2. 如果此时的数不等于 `0`,则将其加入队列,继续查找。
62+
1. 如果此时的数等于 0ドル$,则满足题意,返回当前树的最小深度。
63+
2. 如果此时的数不等于 0ドル$,则将其加入队列,继续查找。
6464

6565
### 思路 1:代码
6666

@@ -97,3 +97,57 @@ class Solution:
9797
- **时间复杂度**:$O(n \times \sqrt{n})$。
9898
- **空间复杂度**:$O(n)$。
9999

100+
### 思路 2:动态规划
101+
102+
我们可以将这道题转换为「完全背包问题」中恰好装满背包的方案数问题。
103+
104+
1. 将 $k = 1, 4, 9, 16, ...$ 看做是 $k$ 种物品,每种物品都可以无限次使用。
105+
2. 将 $n$ 看做是背包的装载上限。
106+
3. 这道题就变成了,从 $k$ 种物品中选择一些物品,装入装载上限为 $n$ 的背包中,恰好装满背包最少需要多少件物品。
107+
108+
###### 1. 划分阶段
109+
110+
按照当前背包的载重上限进行阶段划分。
111+
112+
###### 2. 定义状态
113+
114+
定义状态 $dp[w]$ 表示为:从完全平方数中挑选一些数,使其和恰好凑成 $w$ ,最少需要多少个完全平方数。
115+
116+
###### 3. 状态转移方程
117+
118+
$dp[w] = min \lbrace dp[w], dp[w - num] + 1$
119+
120+
###### 4. 初始条件
121+
122+
- 恰好凑成和为 0ドル,ドル最少需要 0ドル$ 个完全平方数。
123+
- 默认情况下,在不使用完全平方数时,都不能恰好凑成和为 $w$ ,此时将状态值设置为一个极大值(比如 $n + 1$),表示无法凑成。
124+
125+
###### 5. 最终结果
126+
127+
根据我们之前定义的状态,$dp[w]$ 表示为:将物品装入装载上限为 $w$ 的背包中,恰好装满背包,最少需要多少件物品。 所以最终结果为 $dp[n]$。
128+
129+
1. 如果 $dp[n] \ne n + 1,ドル则说明:$dp[n]$ 为装入装载上限为 $n$ 的背包,恰好装满背包,最少需要的物品数量,则返回 $dp[n]$。
130+
2. 如果 $dp[n] = n + 1,ドル则说明:无法恰好装满背包,则返回 $-1$。因为 $n$ 肯定能由 $n$ 个 1ドル$ 组成,所以这种情况并不会出现。
131+
132+
### 思路 2:代码
133+
134+
```Python
135+
class Solution:
136+
def numSquares(self, n: int) -> int:
137+
dp = [n + 1 for _ in range(n + 1)]
138+
dp[0] = 0
139+
140+
for i in range(1, int(sqrt(n)) + 1):
141+
num = i * i
142+
for w in range(num, n + 1):
143+
dp[w] = min(dp[w], dp[w - num] + 1)
144+
145+
if dp[n] != n + 1:
146+
return dp[n]
147+
return -1
148+
```
149+
150+
### 思路 2:复杂度分析
151+
152+
- **时间复杂度**:$O(n \times \sqrt{n})$。
153+
- **空间复杂度**:$O(n)$。

‎Solutions/0322. 零钱兑换.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class Solution:
8282

8383
### 思路 2:完全背包问题
8484

85-
这道题可以转换为:有 $n$ 种不同的硬币,$coins[i]$ 表示第 $i$ 种硬币的面额,每种硬币可以无限次使用。请问凑成总金额为 $amount$ 的背包,最少需要多少硬币?
85+
这道题可以转换为:有 $n$ 种不同的硬币,$coins[i]$ 表示第 $i$ 种硬币的面额,每种硬币可以无限次使用。请问恰好凑成总金额为 $amount$ 的背包,最少需要多少硬币?
8686

8787
与普通完全背包问题不同的是,这里求解的是最少硬币数量。我们可以改变一下「状态定义」和「状态转移方程」。
8888

@@ -107,11 +107,15 @@ $dp[c] = \begin{cases} dp[c] & c < coins[i - 1] \cr min \lbrace dp[c], dp[c - co
107107
###### 4. 初始条件
108108

109109
- 凑成总金额为 0ドル$ 的最少硬币数量为 0ドル,ドル即 $dp[0] = 0$。
110+
- 默认情况下,在不使用硬币时,都不能恰好凑成总金额为 $w$ ,此时将状态值设置为一个极大值(比如 $n + 1$),表示无法凑成。
110111

111112
###### 5. 最终结果
112113

113114
根据我们之前定义的状态,$dp[c]$ 表示为:凑成总金额为 $c$ 的最少硬币数量。则最终结果为 $dp[amount]$。
114115

116+
1. 如果 $dp[amount] \ne amount + 1,ドル则说明: $dp[amount]$ 为凑成金额 $amount$ 的最少硬币数量,则返回 $dp[amount]$。
117+
2. 如果 $dp[amount] = amount + 1,ドル则说明:无法凑成金额 $amount,ドル则返回 $-1$。
118+
115119
### 思路 2:代码
116120

117121
```Python

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /