Python Elegant & Short | Max Heap
Complexity\n- Time complexity: O(n * \log_2 {n})\n- Space complexity: O(n)\n\n# Maximum heap code\n\nclass MaxHeap:\n    def __init__(self, data: List[int]):\n 
# Complexity\n- Time complexity: $$O(n * \\log_2 {n})$$\n- Space complexity: $$O(n)$$\n\n# Maximum heap code\n```\nclass MaxHeap:\n    def __init__(self, data: List[int]):\n        self.data = [-num for num in data]\n        heapq.heapify(self.data)\n\n    def push(self, item: int):\n        heapq.heappush(self.data, -item)\n\n    def pop(self) -> int:\n        return -heapq.heappop(self.data)\n\n    def __len__(self) -> int:\n        return len(self.data)\n\n    def __bool__(self) -> bool:\n        return len(self) != 0\n```\n\n# Solution code\n```\nclass Solution:\n    def lastStoneWeight(self, stones: List[int]) -> int:\n        heap = MaxHeap(stones)\n\n        while len(heap) > 1:\n            first, second = heap.pop(), heap.pop()\n            if first != second:\n                heap.push(abs(first - second))\n\n        return heap.pop() if heap else 0\n```
['Heap (Priority Queue)', 'Python', 'Python3']