From 71980306bf6f653145c0c0f0313a02bd8dd1a77f Mon Sep 17 00:00:00 2001 From: jianghongcheng <35664721+jianghongcheng@users.noreply.github.com> Date: Fri, 23 Jun 2023 12:58:24 -0500 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 --- problems/1049.最后一块石头的重量II.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/1049.最后一块石头的重量II.md b/problems/1049.最后一块石头的重量II.md index a978b802..932029ab 100644 --- a/problems/1049.最后一块石头的重量II.md +++ b/problems/1049.最后一块石头的重量II.md @@ -238,6 +238,21 @@ class Solution: return total_sum - dp[target] - dp[target] +``` + +卡哥版(简化版) +```python +class Solution: + def lastStoneWeightII(self, stones): + total_sum = sum(stones) + target = total_sum // 2 + dp = [0] * (target + 1) + for stone in stones: + for j in range(target, stone - 1, -1): + dp[j] = max(dp[j], dp[j - stone] + stone) + return total_sum - 2* dp[-1] + + ``` 二维DP版 ```python