학습 주제
힙을 이용한 코드 구현
파이썬 내장 함수 heapq 이용
학습 내용
import heapq
def solution(scoville, K):
answer = 0
heapq.heapify(scoville)
while True:
min1 = heapq.heappop(scoville)
if min1 >= K:
break
elif len(scoville) == 0:
answer = -1
break
min2 = heapq.heappop(scoville)
new_scoville = min1 + 2 * min2
heapq.heappush(scoville, new_scoville)
answer += 1
return answer
강의를 듣고 난 후로 크게 어려운 점은 없었다. 이후 다시 풀 때 내장 힙을 편하게 쓸 수 있을 정도의 메서드 기능, 그리고 이번엔 다루지 않았지만 최대 힙도 풀어보면 좋겠다.