Scribbling

프로그래머스: 더 맵게 본문

Computer Science/Coding Test

프로그래머스: 더 맵게

focalpoint 2021. 10. 16. 16:10
import heapq
import copy

def solution(scoville, K):
    answer = 0
    h = copy.deepcopy(scoville)
    heapq.heapify(h)
    while len(h) >= 2 and h[0] < K:
        f1 = heapq.heappop(h)
        f2 = heapq.heappop(h)
        heapq.heappush(h, (f1+2*f2))
        answer += 1
    if h[0] >= K:
        return answer
    else:
        return -1