Snippets Collections
class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        stones = [-s for s in stones] 
        while len(stones) > 1: 
            first = abs(heapq.heappop(stones)) #linear time for python 
            second = abs(heapq.heappop(stones))
            if first > second: 
                heapq.heappush(stones, second - first) #remember that since it's a minHeap we need negative numbers so it's second - first 
        #edge case to account if stones is empty whether originally or after smashing 
        return abs(stones[0])

Tue Aug 16 2022 06:14:12 GMT+0000 (Coordinated Universal Time)

#python #neetcode #minheap #maxheap #pop #push #heapify

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension