[프로그래머스] 더 맵게

yewon Lee·2023년 6월 5일
0


😎코딩테스트 연습>힙(Heap)>더 맵게


📘 문제풀이

런타임 에러는 heapq를 이용해야 해결

import heapq

def solution(scoville, K):
    heapq.heapify(scoville) 
    n = 0
    
    while len(scoville) > 1:
        q1 = heapq.heappop(scoville)
        if q1 >= K:
            break 
        
        q2 = heapq.heappop(scoville)
        heapq.heappush(scoville, q1+q2*2)
        
        n += 1
    if heapq.heappop(scoville) < K:
        return -1
    return n
6,9,10,11,12,13 테스트 오류는 

모든 음식의 스코빌 지수를 K 이상으로 만들 수 없는 경우에는 -1을 return 합니다.

이 조건을 안맞춰서 생김
profile
시작

0개의 댓글