You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution:
deflastStoneWeight(self, stones: List[int]) ->int:
# Python only has a minHeap, so convert `stones` to negative numbers so we can treat it as a maxHeapstones= [-sforsinstones]
heapq.heapify(stones) # Convert the input list to a heapwhilelen(stones) >1:
# `heapPop()` pops and returns the smallest value from the heap, # but since `stones` has been converted to negative numbers it is actually the largest stoney=heapq.heappop(stones) # `y` is the largest stonex=heapq.heappop(stones) # `x` is the second largest stone# If the weights are different, `x` is smashed and `y` becomes `x - y`ifx!=y:
heapq.heappush(stones, y-x)
# The absolute value of the final element must be returned since `stones` was converted to negative numbersreturnabs(stones[0]) ifstoneselse0
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: