본문 바로가기
알고리즘/프로그래머스 level 1,2

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

by 코딩개미뚠뚠 2021. 5. 3.
반응형
def solution(scoville, K):
    import heapq
    data = []
    for s in scoville:
        heapq.heappush(data, s)
    answer = 0
    while len(data) >0:
        if data[0] >= K:
            return answer
        a= heapq.heappop(data)
        if data != []:
            b =heapq.heappop(data)
            heapq.heappush(data,a + (b *2))
        answer +=1    
    return -1

<내 풀이>

heapq를 이용하여 푼 문제이다.

다른 방식으로도 풀어봤는데 시간초과가 떠서 불가능했다.

힙 문제는 많이 안풀어봐서 아직 익숙하지 않다.

반응형

댓글