[프로그래머스] 더 맵게 - python

코린이·2022년 6월 6일
0

프로그래머스

목록 보기
14/22

📢 "더 맵게" 문제

프로그래머스 문제 링크

🔎 풀이

사용언어 : python

heapq 모듈 사용
heapq.heapify(scoville) : 기존 list를 힙으로 변환
heapq.heappop(scoville) : scoville의 최솟값 반환 및 삭제
len(scoville) == 1 and scoville[0] < K 은 더이상 스코빌 지수를 K 이상 만들 수 없는 경우이므로 -1을 return

🔎 코드

import heapq

def solution(scoville, K):
    heapq.heapify(scoville)
    count = 0
    while scoville[0] < K:
        min = heapq.heappop(scoville)
        min2 = heapq.heappop(scoville)
        result = min + (min2 * 2)
        heapq.heappush(scoville, result)
        count += 1
        if len(scoville) == 1 and scoville[0] < K:
            return -1
    return count
profile
초보 개발자

0개의 댓글