본문 바로가기
Data Engineering/Python

[코테] 프로그래머스 - 더 맵게 Python 코드

by strongstar 2025. 9. 18.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import heapq
 
def solution(scoville, K):
    answer = 0
    heapq.heapify(scoville)
    while True:
        min1 = heapq.heappop(scoville)
        if min1 >= K:
            break
        elif len(scoville) == 0:
            answer = -1
            break
        
        min2 = heapq.heappop(scoville)
        new_scoville = min1 + 2*min2
        heapq.heappush(scoville, new_scoville)
        answer += 1
        
    return answer
 
 
# 프로그래머스 - 더 맵게
# https://school.programmers.co.kr/learn/courses/30/lessons/42626
cs