공부/Algorithm

[HackerRank] Jesse and Cookies

김샤랑 2022. 3. 9. 10:19

[HackerRank]  Jesse and Cookies


  • 문제 링크

https://www.hackerrank.com/challenges/jesse-and-cookies/problem?isFullScreen=true 

 


  • 코드
def cookies(k, A):
    import heapq
    
    cnt = 0
    heapq.heapify(A) #A: list -> heap
    while(A[0]<k and len(A)>1):
        sweetness = heapq.heappop(A) + 2*heapq.heappop(A)
        heapq.heappush(A,sweetness)
        cnt += 1

    if(A[0] < k):
        return -1
    return cnt