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
'LeetCode > 주제별 보충' 카테고리의 다른 글
Heap-PrioiryQueue: 215. Kth Largest Element in an Array ★ (0) | 2025.01.20 |
---|---|
Heap-PrioiryQueue: 973. K Closest Points to Origin ★ (0) | 2025.01.20 |
Heap-PrioiryQueue: 703. Kth Largest Element in a Stream ★ (0) | 2025.01.20 |
Backtracking: 51. N-Queens (0) | 2025.01.19 |
Backtracking: 131. Palindrome Partitioning (0) | 2025.01.19 |