forked from labuladong/fucking-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74a3d06
commit b4a38fb
Showing
78 changed files
with
2,431 additions
and
2,283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
# 动态规划之KMP字符匹配算法 | ||
|
||
|
||
<p align='center'> | ||
<a href="https://github.com/labuladong/fucking-algorithm" target="view_window"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a> | ||
<a href="https://www.zhihu.com/people/labuladong"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%[email protected]?style=flat-square&logo=Zhihu"></a> | ||
<a href="https://i.loli.net/2020/10/10/MhRTyUKfXZOlQYN.jpg"><img src="https://img.shields.io/badge/公众号[email protected]?style=flat-square&logo=WeChat"></a> | ||
<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站[email protected]?style=flat-square&logo=Bilibili"></a> | ||
</p> | ||
|
||
![](../pictures/souyisou.png) | ||
|
||
相关推荐: | ||
* [经典动态规划:最长公共子序列](https://labuladong.gitbook.io/algo) | ||
* [特殊数据结构:单调栈](https://labuladong.gitbook.io/algo) | ||
|
||
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: | ||
|
||
[28.实现 strStr()](https://leetcode-cn.com/problems/implement-strstr) | ||
|
||
**-----------** | ||
|
||
KMP 算法(Knuth-Morris-Pratt 算法)是一个著名的字符串匹配算法,效率很高,但是确实有点复杂。 | ||
|
||
很多读者抱怨 KMP 算法无法理解,这很正常,想到大学教材上关于 KMP 算法的讲解,也不知道有多少未来的 Knuth、Morris、Pratt 被提前劝退了。有一些优秀的同学通过手推 KMP 算法的过程来辅助理解该算法,这是一种办法,不过本文要从逻辑层面帮助读者理解算法的原理。十行代码之间,KMP 灰飞烟灭。 | ||
|
@@ -37,7 +57,7 @@ int search(String pat, String txt) { | |
} | ||
``` | ||
|
||
对于暴力算法,如果出现不匹配字符,同时回退 `txt` 和 `pat` 的指针,嵌套 for 循环,时间复杂度 $O(MN)$,空间复杂度$O(1)$。最主要的问题是,如果字符串中重复的字符比较多,该算法就显得很蠢。 | ||
对于暴力算法,如果出现不匹配字符,同时回退 `txt` 和 `pat` 的指针,嵌套 for 循环,时间复杂度 `O(MN)`,空间复杂度`O(1)`。最主要的问题是,如果字符串中重复的字符比较多,该算法就显得很蠢。 | ||
|
||
比如 txt = "aaacaaab" pat = "aaab": | ||
|
||
|
@@ -399,12 +419,14 @@ public class KMP { | |
|
||
KMP 算法也就是动态规划那点事,我们的公众号文章目录有一系列专门讲动态规划的,而且都是按照一套框架来的,无非就是描述问题逻辑,明确 `dp` 数组含义,定义 base case 这点破事。希望这篇文章能让大家对动态规划有更深的理解。 | ||
|
||
**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**: | ||
|
||
![labuladong](../pictures/labuladong.png) | ||
|
||
[上一篇:贪心算法之区间调度问题](../动态规划系列/贪心算法之区间调度问题.md) | ||
**_____________** | ||
|
||
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 | ||
|
||
[下一篇:团灭 LeetCode 股票买卖问题](../动态规划系列/团灭股票问题.md) | ||
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 | ||
|
||
[目录](../README.md#目录) | ||
<p align='center'> | ||
<img src="../pictures/table_qr2.jpg" width=500 > | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,26 @@ | ||
# 动态规划之博弈问题 | ||
|
||
上一篇文章 [几道智力题](../高频面试系列/一行代码解决的智力题.md) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。 | ||
|
||
<p align='center'> | ||
<a href="https://github.com/labuladong/fucking-algorithm" target="view_window"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a> | ||
<a href="https://www.zhihu.com/people/labuladong"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%[email protected]?style=flat-square&logo=Zhihu"></a> | ||
<a href="https://i.loli.net/2020/10/10/MhRTyUKfXZOlQYN.jpg"><img src="https://img.shields.io/badge/公众号[email protected]?style=flat-square&logo=WeChat"></a> | ||
<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站[email protected]?style=flat-square&logo=Bilibili"></a> | ||
</p> | ||
|
||
![](../pictures/souyisou.png) | ||
|
||
相关推荐: | ||
* [40张图解:TCP三次握手和四次挥手面试题](https://labuladong.gitbook.io/algo) | ||
* [如何计算完全二叉树的节点数](https://labuladong.gitbook.io/algo) | ||
|
||
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: | ||
|
||
[877.石子游戏](https://leetcode-cn.com/problems/stone-game) | ||
|
||
**-----------** | ||
|
||
上一篇文章 [几道智力题](https://labuladong.gitbook.io/algo) 中讨论到一个有趣的「石头游戏」,通过题目的限制条件,这个游戏是先手必胜的。但是智力题终究是智力题,真正的算法问题肯定不会是投机取巧能搞定的。所以,本文就借石头游戏来讲讲「假设两个人都足够聪明,最后谁会获胜」这一类问题该如何用动态规划算法解决。 | ||
|
||
博弈类问题的套路都差不多,下文举例讲解,其核心思路是在二维 dp 的基础上使用元组分别存储两个人的博弈结果。掌握了这个技巧以后,别人再问你什么俩海盗分宝石,俩人拿硬币的问题,你就告诉别人:我懒得想,直接给你写个算法算一下得了。 | ||
|
||
|
@@ -43,7 +63,7 @@ dp[0][1].fir = 9 意味着:面对石头堆 [3, 9],先手最终能够获得 9 | |
dp[1][3].sec = 2 意味着:面对石头堆 [9, 1, 2],后手最终能够获得 2 分。 | ||
``` | ||
|
||
我们想求的答案是先手和后手最终分数之差,按照这个定义也就是 $dp[0][n-1].fir - dp[0][n-1].sec$,即面对整个 piles,先手的最优得分和后手的最优得分之差。 | ||
我们想求的答案是先手和后手最终分数之差,按照这个定义也就是 `dp[0][n-1].fir - dp[0][n-1].sec`,即面对整个 piles,先手的最优得分和后手的最优得分之差。 | ||
|
||
### 二、状态转移方程 | ||
|
||
|
@@ -183,41 +203,14 @@ int stoneGame(int[] piles) { | |
|
||
希望本文对你有帮助。 | ||
|
||
**致力于把算法讲清楚!欢迎关注我的微信公众号 labuladong,查看更多通俗易懂的文章**: | ||
|
||
![labuladong](../pictures/labuladong.png) | ||
|
||
[Hanmin](https://github.com/Miraclemin/) 提供 Python3 代码: | ||
|
||
```python | ||
def stoneGame(self, piles:List[int]) -> int: | ||
n = len(piles) | ||
##初始化dp数组,用三维数组表示 | ||
dp = [[[0,0] for _ in range(0,n)] for _ in range(n)] | ||
##填入base case | ||
for i in range(0,n): | ||
dp[i][i][0] = piles[i] | ||
dp[i][i][1] = 0 | ||
##斜着遍历数组 | ||
for l in range(2,n+1): | ||
for i in range(0,n-l+1): | ||
j = l + i - 1 | ||
##先手选择最左边或者最右边的分数 | ||
left = piles[i] + dp[i+1][j][1] | ||
right = piles[j] + dp[i][j-1][1] | ||
##套用状态转移方程 | ||
if left > right: | ||
dp[i][j][0] = left | ||
dp[i][j][1] = dp[i+1][j][0] | ||
else: | ||
dp[i][j][0] = right | ||
dp[i][j][1] = dp[i][j-1][0] | ||
res = dp[0][n-1] | ||
return res[0] - res[1] | ||
``` | ||
**_____________** | ||
|
||
[上一篇:动态规划之子序列问题解题模板](../动态规划系列/子序列问题模板.md) | ||
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 | ||
|
||
[下一篇:贪心算法之区间调度问题](../动态规划系列/贪心算法之区间调度问题.md) | ||
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 | ||
|
||
[目录](../README.md#目录) | ||
<p align='center'> | ||
<img src="../pictures/table_qr2.jpg" width=500 > | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,27 @@ | ||
# 动态规划之四键键盘 | ||
|
||
|
||
<p align='center'> | ||
<a href="https://github.com/labuladong/fucking-algorithm" target="view_window"><img alt="GitHub" src="https://img.shields.io/github/stars/labuladong/fucking-algorithm?label=Stars&style=flat-square&logo=GitHub"></a> | ||
<a href="https://www.zhihu.com/people/labuladong"><img src="https://img.shields.io/badge/%E7%9F%A5%E4%B9%[email protected]?style=flat-square&logo=Zhihu"></a> | ||
<a href="https://i.loli.net/2020/10/10/MhRTyUKfXZOlQYN.jpg"><img src="https://img.shields.io/badge/公众号[email protected]?style=flat-square&logo=WeChat"></a> | ||
<a href="https://space.bilibili.com/14089380"><img src="https://img.shields.io/badge/B站[email protected]?style=flat-square&logo=Bilibili"></a> | ||
</p> | ||
|
||
![](../pictures/souyisou.png) | ||
|
||
相关推荐: | ||
* [如何高效寻找素数](https://labuladong.gitbook.io/algo) | ||
* [动态规划解题套路框架](https://labuladong.gitbook.io/algo) | ||
|
||
读完本文,你不仅学会了算法套路,还可以顺便去 LeetCode 上拿下如下题目: | ||
|
||
[651.四键键盘](https://leetcode-cn.com/problems/4-keys-keyboard) | ||
|
||
**-----------** | ||
|
||
PS:现在这到题好想变成会员题目了?我当时做的时候还是免费的。 | ||
|
||
四键键盘问题很有意思,而且可以明显感受到:对 dp 数组的不同定义需要完全不同的逻辑,从而产生完全不同的解法。 | ||
|
||
首先看一下题目: | ||
|
@@ -168,13 +190,12 @@ def dp(n, a_num, copy): | |
|
||
根据这个事实,我们重新定义了状态,重新寻找了状态转移,从逻辑上减少了无效的子问题个数,从而提高了算法的效率。 | ||
|
||
坚持原创高质量文章,致力于把算法问题讲清楚,欢迎关注我的公众号 labuladong 获取最新文章: | ||
|
||
![labuladong](../pictures/labuladong.jpg) | ||
|
||
**_____________** | ||
|
||
[上一篇:团灭 LeetCode 打家劫舍问题](../动态规划系列/抢房子.md) | ||
**刷算法,学套路,认准 labuladong,公众号和 [在线电子书](https://labuladong.gitbook.io/algo) 持续更新最新文章**。 | ||
|
||
[下一篇:动态规划之正则表达](../动态规划系列/动态规划之正则表达.md) | ||
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。 | ||
|
||
[目录](../README.md#目录) | ||
<p align='center'> | ||
<img src="../pictures/table_qr2.jpg" width=500 > | ||
</p> |
Oops, something went wrong.