[LeetCode] 1962. Remove Stones to Minimize the Total

김민우·2022년 12월 28일
0

알고리즘

목록 보기
100/189

- Problem

1962. Remove Stones to Minimize the Total


- 내 풀이

class Solution:
    def minStoneSum(self, piles: List[int], k: int) -> int:
        heap = []
        answer = 0
        
        for pile in piles:
            heapq.heappush(heap, (-pile, pile))
        
        for _ in range(k):
            x = heapq.heappop(heap)[1]
            x = ceil(x/2)
            heapq.heappush(heap, (-x, x))

        return sum([node[1] for node in heap])

- 결과

- 다른 Heap 풀이

class Solution:
    def minStoneSum(self, piles: List[int], k: int) -> int:
        heap = [-pile for pile in piles]
        heapq.heapify(heap)
        
        for _ in range(k):
            heapq.heapreplace(heap, heap[0] // 2)
        
        return -sum(heap)

- 결과

profile
Pay it forward.

0개의 댓글