Language/Python

[Python / 프로그래머스 level 2] 더 맵게

ej503 2021. 8. 11. 11:55

import heapq
def solution(scoville, K):
    answer = 0
    heapq.heapify(scoville)
    while scoville[0] < K:
        a = heapq.heappop(scoville) + (heapq.heappop(scoville) * 2)
        heapq.heappush(scoville, a)
        answer += 1
        if len(scoville) == 1 and scoville[0] < K:
            return -1
    return answer