본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 더 맵게(python)

by 김홍중 2021. 5. 29.
import heapq

def mix_food(min_food, next_food):
    multi = 2
    return min_food + next_food * multi

def solution(scovilles, K):
    heap = []
    min_food = 0
    next_food = 0
    mix = 0
    
    for scoville in scovilles:
    	heapq.heappush(heap, scoville)
    while True:
        min_food = heapq.heappop(heap)
        if(min_food >= K):
            break
        if(len(heap) == 0):
            return -1
        next_food = heapq.heappop(heap)
        heapq.heappush(heap, mix_food(min_food, next_food))
        mix += 1

    return mix

 

댓글