본문 바로가기

Programmers - 더 맵게 [파이썬] 본문

Algorithms/Heap

Programmers - 더 맵게 [파이썬]

Louisus 2020. 5. 13. 19:47
728x90

출처:

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

import heapq

def solution(scoville, K):
    answer = 0
    heap = []
    for num in scoville:
        heapq.heappush(heap, num)
        
    while heap[0] < K:
        try:
            heapq.heappush(heap,heapq.heappop(heap) + (heapq.heappop(heap)*2))
        except IndexError:
            return -1
        answer += 1
    return answer

Comments