더 맵게(python)

이민호·2021년 3월 17일
0

나의 풀이

import heapq
def solution(scoville, k):
    heapq.heapify(scoville)
    answer = 0
    while scoville[0] < k:
        if len(scoville) > 1:
            new = heapq.heappop(scoville) + (heapq.heappop(scoville) * 2)
            heapq.heappush(scoville,new)
            answer += 1
        else:
            return -1
    return answer

point #1
heap.heapify를 하면 전체 정렬이 되지는 않지막 적어도 리스트의 [0]은 제일 작은 수가 올 수 있게 할 수 있다.

point #2
if len(scoville) > 1: 을 if scoville:로 하면 런타임 오류가 난다?...
-> 둘다 scoville 안에 요소가 있는 것을 의미하는 것이 아닌가??

알게된 점

heap의 용법에 대해 자세하게 알게 되었다.
그러나 len(scoville) > 1 과 if scoville: 의 차이점을 아직 알아내지 못하였다...

profile
life is fun

0개의 댓글