Solution 1 - Max Heap
Always smash the two heaviest stones.
Always smash the two heaviest stones.
""" 1046.1 - Last Stone Weight - Solution 1 - Max Heap """
import heapq
from typing import List
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
h = [-x for x in stones]
heapq.heapify(h)
while len(h) > 1:
y = -heapq.heappop(h)
x = -heapq.heappop(h)
if y != x:
heapq.heappush(h, -(y - x))
return -h[0] if h else 0