Update 1049.最后一块石头的重量II.md

This commit is contained in:
jianghongcheng 2023-06-23 12:58:24 -05:00 committed by GitHub
parent 35a87bd56c
commit 71980306bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -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