본문 바로가기

프로그래머스 알고리즘 문제/Level 2

프로그래머스 Level 2 - 더 맵게

내 정답:

 

import heapq

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

 

다른사람들의 정답을 보아도 대부분 heapq 모듈을 이용하여 구현했다.