From bc14b2e06201be75cf4a28b8d463e8e1f83f6608 Mon Sep 17 00:00:00 2001 From: hzs Date: Wed, 11 Nov 2020 17:50:05 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90877.=E7=9F=B3=E5=AD=90=E6=B8=B8?= =?UTF-8?q?=E6=88=8F=E3=80=90Python=E3=80=91=E4=B8=80=E7=BB=B4dp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...232\345\274\210\351\227\256\351\242\230.md" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" index c85c100..90615ba 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222\347\263\273\345\210\227/\345\212\250\346\200\201\350\247\204\345\210\222\344\271\213\345\215\232\345\274\210\351\227\256\351\242\230.md" @@ -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 + + +``` + -- GitLab