Solution 1

  • TimeO(n log n)
  • SpaceO(n)

where, n is the length of nums
Solved in 5 mins, all by yourself! Good job!

Python
from heapq import heapify_max, heappush_max, heappop_max
class Solution:
    '''
    Time Complexity: O(n log n)
    Space Complexity: O(n)
    where, n is the length of nums
    Solved in 5 mins, all by yourself! Good job!
    '''
    def lastStoneWeight(self, stones: List[int]) -> int:
        heapify_max(stones)
        while len(stones) > 1:
            stone1 = heappop_max(stones)
            stone2 = heappop_max(stones)
            if stone1 != stone2:
                heappush_max(stones, stone1-stone2)
        return stones[0] if stones else 0
Leet Code/python.py · L2095–2111