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 abbfe39

Browse files
update content
1 parent 9ad94cc commit abbfe39

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

‎动态规划系列/动态规划详解进阶.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ int fib(int N) {
198198

199199
这个例子的最后,讲一个细节优化。
200200

201-
细心的读者会发现,根据斐波那契数列的状态转移方程,当前状态只和之前的两个状态有关,其实并不需要那么长的一个 DP table 来存储所有的状态,只要想办法存储之前的两个状态就行了。
201+
细心的读者会发现,根据斐波那契数列的状态转移方程,当前状态 `n` 只和之前的 `n-1, n-2` 两个状态有关,其实并不需要那么长的一个 DP table 来存储所有的状态,只要想办法存储之前的两个状态就行了。
202202

203203
所以,可以进一步优化,把空间复杂度降为 O(1)。这也就是我们最常见的计算斐波那契数的算法:
204204

@@ -224,7 +224,7 @@ int fib(int n) {
224224

225225
这一般是动态规划问题的最后一步优化,如果我们发现每次状态转移只需要 DP table 中的一部分,那么可以尝试缩小 DP table 的大小,只记录必要的数据,从而降低空间复杂度。
226226

227-
上述例子就相当于把DPtable 的大小从 `n` 缩小到 2后续的动态规划章节中我们还会看到这样的例子,一般来说是把一个二维的 DP table 压缩成一维,即把空间复杂度从 O(n^2) 压缩到 O(n)。
227+
上述例子就相当于把 DPtable 的大小从 `n` 缩小到 2我会在后文 [对动态规划发动降维打击](https://labuladong.github.io/article/fname.html?fname=状态压缩技巧) 进一步讲解这个压缩空间复杂度的技巧,一般来说用来把一个二维的 DP table 压缩成一维,即把空间复杂度从 O(n^2) 压缩到 O(n)。
228228

229229
有人会问,动态规划的另一个重要特性「最优子结构」,怎么没有涉及?下面会涉及。斐波那契数列的例子严格来说不算动态规划,因为没有涉及求最值,以上旨在说明重叠子问题的消除方法,演示得到最优解法逐步求精的过程。下面,看第二个例子,凑零钱问题。
230230

‎动态规划系列/状态压缩技巧.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ int longestPalindromeSubseq(string s) {
233233
234234
- [一个方法团灭 LeetCode 股票买卖问题](https://labuladong.github.io/article/fname.html?fname=团灭股票问题)
235235
- [动态规划之最小路径和](https://labuladong.github.io/article/fname.html?fname=最小路径和)
236+
- [动态规划解题套路框架](https://labuladong.github.io/article/fname.html?fname=动态规划详解进阶)
236237
- [动态规划设计:最大子数组](https://labuladong.github.io/article/fname.html?fname=最大子数组)
237238
- [我的刷题心得](https://labuladong.github.io/article/fname.html?fname=算法心得)
238239
- [经典动态规划:子集背包问题](https://labuladong.github.io/article/fname.html?fname=背包子集)

0 commit comments

Comments
(0)

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