提交 bc14b2e0 编写于 作者: H hzs 提交者: labuladong

【877.石子游戏【Python】一维dp

上级 97e5e1de
......@@ -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
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册