Leetcode 1127 Solution

This article provides solution to leetcode question 1127 (last-stone-weight)

https://leetcode.com/problems/last-stone-weight

Solution

class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        heap = []

        for stone in stones:
            heapq.heappush(heap, -stone)

        while len(heap) > 1:
            stone1 = -heapq.heappop(heap)
            stone2 = -heapq.heappop(heap)

            heapq.heappush(heap, stone2 - stone1)

        return -heapq.heappop(heap)