heapify★heappush★힙★더 맵게★[프로그래머스]
2023. 1. 3. 14:41
728x90
반응형
https://school.programmers.co.kr/learn/courses/30/lessons/42626?language=python3
VERSION 1.0
import heapq
from collections import deque
from heapq import *
def solution(scoville, K):
count = 0
heapify(scoville)
print(f'heapify(scoville) : {scoville}')
while scoville[0] < K and len(scoville) > 1:
num1 = heappop(scoville)
num2 = heappop(scoville)
heappush(scoville, num1 + num2 * 2)
print(scoville)
count += 1
return count if scoville[0] >= K else -1
==> heapify ==> 오름차순 정렬!!
==> from heapq import * ==> heapq의 모든 내장함수 가져온다.
==> heappush 주목!
728x90
반응형