동캄의 코딩도장

프로그래머스 level2 [더 맵게] 파이썬 본문

코테/프로그래머스

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

동 캄 2021. 12. 11. 12:15

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

 

코딩테스트 연습 - 더 맵게

매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같

programmers.co.kr

#프로그래머스 더 맵게
def solution(scoville, K):
    import heapq
    answer = 0
    heapq.heapify(scoville)
    leng=len(scoville)
    while scoville[0]<K and leng>1:
        x1=heapq.heappop(scoville)
        x2=heapq.heappop(scoville)
        new= x1+x2*2
        heapq.heappush(scoville,new)
        leng-=1
        answer+=1
    if scoville[0]>=K:
        return answer
    else:
        return -1

파이썬 heapq 내장함수를 이용하여 해결하였다.

*알게된사실: (걸리는 시간 pop()>heappop()이다.)