[알고리즘/프로그래머스] - 더 맵게(python)

유현민·2022년 6월 9일
0

알고리즘

목록 보기
204/253

문제

heapq를 사용해서 풀어야하는 문제였다.
정렬이 자동으로 되기 때문에. while문을 써서 구하면 된다.

import heapq


def solution(scoville, K):
    answer = 0
    heapq.heapify(scoville)

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

    return answer
profile
smilegate megaport infra

0개의 댓글