From 276a7b1e14ca7a54ac9826bde248e0a1c6833444 Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Mon, 31 May 2021 21:34:11 +0800 Subject: [PATCH] =?UTF-8?q?Update=201049.=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E5=9D=97=E7=9F=B3=E5=A4=B4=E7=9A=84=E9=87=8D=E9=87=8FII.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- ...244\264\347\232\204\351\207\215\351\207\217II.md" | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" index fcd3712a53..f74600b1d1 100644 --- "a/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" +++ "b/problems/1049.\346\234\200\345\220\216\344\270\200\345\235\227\347\237\263\345\244\264\347\232\204\351\207\215\351\207\217II.md" @@ -178,7 +178,17 @@ class Solution { ``` Python: - +```python +class Solution: + def lastStoneWeightII(self, stones: List[int]) -> int: + sumweight = sum(stones) + target = sumweight // 2 + dp = [0] * 15001 + for i in range(len(stones)): + for j in range(target, stones[i] - 1, -1): + dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]) + return sumweight - 2 * dp[target] +``` Go: