코테 준비/Heap

[프로그래머스] 더 맵게

imsmile2000 2023. 1. 25. 11:45
import heapq
def solution(scoville, K):
    count=0
    heapq.heapify(scoville) #이부분을 안해줘서 계속 오류가 남
    while scoville[0]<K: #min(scoville)<K라고 하면 효율성에서 계속 0점이 뜸 scoville[0]이라고 해야함
        a=heapq.heappop(scoville)
        b=heapq.heappop(scoville)
        heapq.heappush(scoville,a+(b*2))
        count+=1
        if len(scoville)==1 and scoville[0]<K:
            return -1
            break
    return count