본문 바로가기

Coding Test/Programmers

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

728x90

 

 

나의 풀이

import heapq

def solution(scoville, K):
    count = 0
    heapq.heapify(scoville)

    while scoville[0] < K:
        try:
            heapq.heappush(scoville, heapq.heappop(scoville) + 2 * heapq.heappop(scoville))
            count += 1
        except:
            return -1

    return count

 

 

 

인덱스를 활용한 풀이

from heapq import heapify, heappush, heappop
def solution(scoville, K):
    heapify(scoville)
    for i in range(1000000):
        try:
            heappush(scoville, heappop(scoville)+(heappop(scoville)*2))
            if scoville[0] >= K: return i+1
        except:
            return -1
반응형