[Programmers] - 더 맵게 (Level 2)


힙(Heap)
LEVEL : 2
문제링크

scoville = [1, 2, 3, 9, 10, 12]
K = 7
def solution(scoville, K):
    import heapq

    if sum(scoville) == 0:
        return -1

    heapq.heapify(scoville)
    cnt = 0
    while scoville[0] < K and len(scoville) > 1:

        fmin = heapq.heappop(scoville)
        smin = heapq.heappop(scoville)
        heapq.heappush(scoville, fmin+smin*2)

        cnt+=1

    if scoville[0] < K: return -1
    return cnt
solution(scoville, K)
2

Comments