mirror of
https://github.com/labuladong/fucking-algorithm.git
synced 2025-07-05 03:36:39 +08:00
【877.石子游戏【Python】一维dp
This commit is contained in:
@ -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
|
||||
|
||||
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user