import heapq
def solution(scoville, K):
answer = 0
heapq.heapify(scoville)
heap = scoville
while heap[0] < K and len(heap) >= 2:
first = heapq.heappop(heap)
second = heapq.heappop(heap)
new = first + (second *2)
heapq.heappush(heap,new)
answer+=1
if heap[0] >= K:
return answer
else:
return -1
return answer
원래 학교에서 배울땐 구현하는걸 배우는데 코테에서는 기본 모듈 함수이므로 주워서 쓰면된다.
import heapq : import
heapq.heappush(array, item) : 최소힙에 item 을 삽입하는 함수이다.
heapq.heappop(array) : 최소힙에 root 를 반환한다. 즉 최솟값을 반환함.
heapq.heapify(array) 리스트를 heap로 정렬하는 함수.
정리 아주 좋습니다!!!!
유익하네요 잘 보고 갑니다 ㅎㅎ