LV 2: 더 맵게

ewillwin·2023년 8월 23일
0

문제 링크

LV 2: 더 맵게


구현 방식

  • 두 개의 음식을 섞는 방법: 섞은 음식의 스코빌 지수 = 가장 맵지 않은 음식의 스코빌 지수 + (두 번째로 맵지 않은 음식의 스코빌 지수 * 2)
    -> 정렬된 순서를 유지해야하는 경우이므로 최소힙을 이용해 풀어주었다

코드

import heapq

def solution(scoville, K):
    N = len(scoville)
    
    heap = []
    for i in range(N):
        heapq.heappush(heap, scoville[i])
    
    count = 0
    while True:
        first = heapq.heappop(heap); second = heapq.heappop(heap)
        
        if first >= K: return count
        if len(heap) <= 0:
            if first + (second*2) >= K:
                return count+1
            return -1
               
        heapq.heappush(heap, first + (second*2))
        count += 1
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글