Skip to content

Commit

Permalink
【877.石子游戏【Python】一维dp
Browse files Browse the repository at this point in the history
  • Loading branch information
hzs authored and labuladong committed Nov 11, 2020
1 parent 97e5e1d commit bc14b2e
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions 动态规划系列/动态规划之博弈问题.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,21 @@ class Solution:

```



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

```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


```

0 comments on commit bc14b2e

Please sign in to comment.