Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
heapq.heappushpop solution in Clear category for The Final Stone by juestr
from heapq import heapify, heappop, heappushpop
def final_stone(stones: list[int]) -> int:
if not stones:
return 0
else:
heap = [-s for s in stones]
heapify(heap)
largest = heappop(heap)
while heap:
nextlargest = heappop(heap)
largest = heappushpop(heap, largest - nextlargest)
return -largest
July 26, 2022
Comments: