思路1:
使用heapq库构造小根堆。
把石头的重量取负,构造小根堆。
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
import heapq
stones = [-weight for weight in stones] # 取负
heapq.heapify(stones) # 原地创建小根堆
while len(stones)>1:
x = heapq.heappop(stones)
y = heapq.heappop(stones)
if x!=y:
heapq.heappush(stones,-abs(y-x))
if stones:return -stones[0]
else:return 0