Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 into master
  • Loading branch information
ningwei.shi committed Nov 12, 2020
2 parents 4fa39a5 + 36f59b6 commit cfdfa28
Show file tree
Hide file tree
Showing 66 changed files with 355 additions and 84 deletions.
4 changes: 3 additions & 1 deletion 动态规划系列/动态规划之KMP字符匹配算法.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,5 +428,7 @@ KMP 算法也就是动态规划那点事,我们的公众号文章目录有一
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
75 changes: 74 additions & 1 deletion 动态规划系列/动态规划之博弈问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,78 @@ int stoneGame(int[] piles) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======





python3版本

由[SCUHZS](https://github.com/brucecat)提供

这里采取的是三维的做法

```python
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
n = len(piles)

# 初始化一个n*n的矩阵 dp数组
dp = [[None] * n for i in range(0, n)]

# 在三角区域填充
for i in range(n):
for j in range(i, n):
dp[i][j] = [0, 0]

# 填入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] > 0

```



压缩成一维数组,以减小空间复杂度,做法如下。

```python
class Solution:
def stoneGame(self, piles: List[int]) -> bool:
dp = piles.copy()

for i in range(len(piles) - 1, -1, -1): # 从下往上遍历
for j in range(i, len(piles)): # 从前往后遍历
dp[j] = max(piles[i] - dp[j], piles[j] - dp[j - 1]) # 计算之后覆盖一维数组的对应位置

return dp[len(piles) - 1] > 0


```

4 changes: 3 additions & 1 deletion 动态规划系列/动态规划之四键键盘.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,7 @@ def dp(n, a_num, copy):
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
3 changes: 2 additions & 1 deletion 动态规划系列/动态规划之正则表达.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ bool dp(string& s, int i, string& p, int j) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,7 @@ public int lengthOfLIS(int[] nums) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/动态规划详解进阶.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,5 +363,7 @@ PS:为啥 `dp` 数组初始化为 `amount + 1` 呢,因为凑成 `amount` 金
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/团灭股票问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,5 +425,7 @@ int maxProfit_k_any(int max_k, int[] prices) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 动态规划系列/子序列问题模板.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 动态规划之子序列问题解题模板

**学好算法全靠套路,认准 labuladong 就够了**


<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>
Expand Down Expand Up @@ -171,5 +171,7 @@ int longestPalindromeSubseq(string s) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,labuladong 带你搞定 LeetCode**。
<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
6 changes: 3 additions & 3 deletions 动态规划系列/抢房子.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# 团灭 LeetCode 打家劫舍问题

**学好算法全靠套路,认准 labuladong 就够了**

<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>
Expand Down Expand Up @@ -257,8 +255,10 @@ int[] dp(TreeNode root) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
[Shantom](https://github.com/Shantom) 提供 198. House Robber I Python3 解法代码:

```Python
Expand Down
6 changes: 4 additions & 2 deletions 动态规划系列/最优子结构.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 动态规划答疑篇

**学好算法全靠套路,认准 labuladong 就够了**


<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>
Expand Down Expand Up @@ -154,5 +154,7 @@ for (int i = 1; i < m; i++)
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/最长公共子序列.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,7 @@ else:
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/编辑距离.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,7 @@ class Node {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/贪心算法之区间调度问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,7 @@ int findMinArrowShots(int[][] intvs) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 动态规划系列/高楼扔鸡蛋进阶.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,5 +292,7 @@ while (lo < hi) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 动态规划系列/高楼扔鸡蛋问题.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 经典动态规划问题:高楼扔鸡蛋

**学好算法全靠套路,认准 labuladong 就够了**


<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>
Expand Down Expand Up @@ -258,5 +258,7 @@ def superEggDrop(self, K: int, N: int) -> int:
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 技术/linuxshell.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,7 @@ $ sudo /home/fdl/bin/connect.sh
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 技术/linux进程.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,7 @@ $ cmd1 | cmd2 | cmd3
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 技术/redis入侵.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,7 @@ Redis 监听的默认端口是 6379,我们设置它接收网卡 127.0.0.1 的
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
</p>
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 技术/session和cookie.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,7 @@ https://github.com/astaxie/build-web-application-with-golang
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
</p>
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 技术/在线练习平台.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,7 @@ https://sqlzoo.net/
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
</p>
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
3 changes: 2 additions & 1 deletion 技术/密码技术.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ HTTPS 协议中的 SSL/TLS 安全层会组合使用以上几种加密方式,**
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
[test ad](https://labuladong.gitbook.io/algo)
4 changes: 3 additions & 1 deletion 数据结构系列/二叉堆详解实现优先级队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,5 +236,7 @@ public Key delMax() {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 数据结构系列/二叉搜索树操作集锦.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,7 @@ void BST(TreeNode root, int target) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 数据结构系列/单调栈.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,7 @@ vector<int> nextGreaterElements(vector<int>& nums) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**。
<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
</p>
<img src="../pictures/qrcode.jpg" width=200 >
</p>
======其他语言代码======
4 changes: 3 additions & 1 deletion 数据结构系列/单调队列.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,7 @@ vector<int> maxSlidingWindow(vector<int>& nums, int k) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
6 changes: 4 additions & 2 deletions 数据结构系列/实现计算器.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 拆解复杂问题:实现计算器

**学好算法全靠套路,认准 labuladong 就够了**


<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>
Expand Down Expand Up @@ -303,5 +303,7 @@ def calculate(s: str) -> int:
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 数据结构系列/设计Twitter.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,7 @@ PS:本文前两张图片和 GIF 是我第一次尝试用平板的绘图软件
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
4 changes: 3 additions & 1 deletion 数据结构系列/递归反转链表的一部分.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,7 @@ ListNode reverseBetween(ListNode head, int m, int n) {
**本小抄即将出版,微信扫码关注公众号,后台回复「小抄」限时免费获取,回复「进群」可进刷题群一起刷题,带你搞定 LeetCode**

<p align='center'>
<img src="../pictures/table_qr2.jpg" width=500 >
<img src="../pictures/qrcode.jpg" width=200 >
</p>

======其他语言代码======
Loading

0 comments on commit cfdfa28

Please sign in to comment.