1046. Last Stone Weight
https://leetcode.com/problems/last-stone-weight/
Python
Max Heap
import heapq
class Solution:
def lastStoneWeight(self, stones: List[int]) -> int:
if len(stones) < 2:
# Constraints indicate the len(stones) min to 1
return stones[0]
# Build the max heap
for i in range(len(stones)):
stones[i] = -stones[i]
heapq.heapify(stones)
while len(stones) > 1:
largest_1 = heapq.heappop(stones) # miniumn native is the largest
largest_2 = heapq.heappop(stones)
if largest_1 != largest_2:
heapq.heappush(stones, largest_1-largest_2)
return -heapq.heappop(stones) if stones else 0