상세 컨텐츠

본문 제목

프로그래머스 더 맵게 파이썬

알고리즘 공부

by 독서와 여행 2021. 3. 23. 15:32

본문

파이썬 내장 함수인 heapq 를 사용하여 동일하게 돌렸더니 되었다.

 

파이썬의 heapq와 heap에 대해 공부를 해서 작성해야지

import heapq
def solution(scoville, K):
    answer = 0
    heapq.heapify(scoville)
    while scoville[0] < K and len(scoville) != 1 :
        answer += 1
        rank = heapq.heappop(scoville) + heapq.heappop(scoville) * 2
        heapq.heappush(scoville,rank)
        
    if len(scoville) == 1 and scoville[0] < K:
        return -1
    
    return answer

관련글 더보기

댓글 영역