Gary Lai
  • Home
  • About
Sign in Subscribe

1046. Last Stone Weight

Last updated on  Dec 28, 2023

1046. Last Stone Weight

class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        heap = []
        for stone in stones:
            heapq.heappush(heap, -1 * stone)
        
        while len(heap) > 1:
            first = heapq.heappop(heap)
            second = heapq.heappop(heap)
            if first != second:
                heapq.heappush(heap, first - second)
        
        if len(heap) == 0:
            return 0
        else:
            return -1 * heap[0]
Previous 703. Kth Largest Element in a Stream
Next 141. Linked List Cycle
Gary Lai © 2025
  • Sign up
Powered by Ghost