코딩테스트/프로그래머스

[프로그래머스] 더 맵게 - 힙, 힙큐

to,min 2024. 10. 25. 17:57

문제 링크 

https://school.programmers.co.kr/learn/courses/30/lessons/42626

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

시간복잡도 Olog(n)

"""
최소 힙의 0 index 가 key 이상일때까지
heapq 는 정렬 되어있지 않고 그냥 최솟값을 찾는 알고리즘임
"""
import heapq
def solution(scoville, K):
    heap = scoville
    heapq.heapify(heap)
    
    answer = 0
    
    while heap[0] < K:
        first = heapq.heappop(heap)
        second = heapq.heappop(heap)
        heapq.heappush(heap,first + (second*2))
        
        if len(heap) < 2:
            if heap[0] < K: return -1
        print(heap)
        answer+=1
    return answer