반응형
문제
https://leetcode.com/problems/take-gifts-from-the-richest-pile/description/
해설
최대 힙을 이용하는 문제였다.
import heapq
class Solution:
def pickGifts(self, gifts: List[int], k: int) -> int:
heap = [-v for v in gifts]
heapq.heapify(heap)
for _ in range(k):
heapq.heapreplace(heap, -int((-heap[0]) ** 0.5))
return -sum(heap)
반응형