LeetCode/주제별 보충

Heap-PrioiryQueue: 1046. Last Stone Weight

hyunkookim 2025. 1. 20. 14:26

1046. Last Stone Weight

 

https://youtu.be/B-QCq79-Vfw?si=3xrwdkwlsVfUMrM8

 

class Solution:
    def lastStoneWeight(self, stones: List[int]) -> int:
        # 기본적으로 minHeap 이니깐, -1 곱하면 maxheap 
        stones = [-s for s in stones]
        heapq.heapify(stones)
        print(stones)

        while len(stones) > 1:
            high_1st = heapq.heappop(stones)
            high_2nd = heapq.heappop(stones)

            if high_1st != high_2nd:
                heapq.heappush(stones, high_1st-high_2nd)

        #  stones 없으면, 0 추가되서, return stones[0], stones 있어도 상관 없음
        # stones.append(0) 
        return abs(stones[0]) if len(stones) == 1 else 0