这道题目是一个01背包,背包可承受质量为总质量的一半,然后找到这个质量就可以了
class Solution:
def lastStoneWeightII(self, stones: List[int]) -> int:
n = len(stones)
s = sum(stones)/2
dp = {0}
for i in range(n):
dp |= {stones[i] + c if stones[i] + c <= s else c for c in dp}
return int((s-max(dp))*2)