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

This commit is contained in:
jianghongcheng
2023-06-04 06:27:03 -05:00
committed by GitHub
parent 285b6ab370
commit 980c98ca0d

View File

@ -224,18 +224,79 @@ class Solution {
```
### Python
卡哥版
```python
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
sumweight = sum(stones)
target = sumweight // 2
dp = [0] * (target + 1)
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]
```
dp = [0] * 15001
total_sum = sum(stones)
target = total_sum // 2
for stone in stones: # 遍历物品
for j in range(target, stone - 1, -1): # 遍历背包
dp[j] = max(dp[j], dp[j - stone] + stone)
return total_sum - dp[target] - dp[target]
```
二维DP版
```python
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
total_sum = sum(stones)
target = total_sum // 2
# 创建二维dp数组行数为石头的数量加1列数为target加1
# dp[i][j]表示前i个石头能否组成总重量为j
dp = [[False] * (target + 1) for _ in range(len(stones) + 1)]
# 初始化第一列表示总重量为0时前i个石头都能组成
for i in range(len(stones) + 1):
dp[i][0] = True
for i in range(1, len(stones) + 1):
for j in range(1, target + 1):
# 如果当前石头重量大于当前目标重量j则无法选择该石头
if stones[i - 1] > j:
dp[i][j] = dp[i - 1][j]
else:
# 可选择该石头或不选择该石头
dp[i][j] = dp[i - 1][j] or dp[i - 1][j - stones[i - 1]]
# 找到最大的重量i使得dp[len(stones)][i]为True
# 返回总重量减去两倍的最接近总重量一半的重量
for i in range(target, -1, -1):
if dp[len(stones)][i]:
return total_sum - 2 * i
return 0
```
一维DP版
```python
class Solution:
def lastStoneWeightII(self, stones):
total_sum = sum(stones)
target = total_sum // 2
dp = [False] * (target + 1)
dp[0] = True
for stone in stones:
for j in range(target, stone - 1, -1):
# 判断当前重量是否可以通过选择之前的石头得到或选择当前石头和之前的石头得到
dp[j] = dp[j] or dp[j - stone]
for i in range(target, -1, -1):
if dp[i]:
# 返回剩余石头的重量,即总重量减去两倍的最接近总重量一半的重量
return total_sum - 2 * i
return 0
```
### Go
```go
func lastStoneWeightII(stones []int) int {