반응형
문제
https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=python3
해설
최소 힙을 활용하는 문제이다.
import heapq
def solution(scoville, K):
heapq.heapify(scoville)
answer = 0
while scoville[0] < K and 1 < len(scoville):
material1 = heapq.heappop(scoville)
material2 = heapq.heappop(scoville)
heapq.heappush(scoville, material1 + material2*2)
answer += 1
if scoville[0] < K:
return -1
return answer
반응형