Problem statement

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

Solution

We just can simulate process, using heaps.

Complexity

It is O(n log n) for time and space.

Code

class Solution:
    def lastStoneWeight(self, stones):
        stones = [-val for val in stones]
        heapify(stones)
        while len(stones) > 1:
            x1 = heappop(stones)
            x2 = heappop(stones)
            if x1 != x2:
                heappush(stones,-abs(x1-x2))
        if len(stones) == 0:
            return 0
        return -stones[0]