[Programmers] P42891 뮤지의 먹방 라이브

Question

Language: Python

풀이 참조

Solution

import heapq    
def solution(food_times, k):
    answer = 0
    if sum(food_times) <=k:
        return -1
    heap=[]
    for i,food in enumerate(food_times):
        heapq.heappush(heap,(food,i+1))
    
    prev=0
    sub_sum=0
    length=len(heap)
    while sub_sum + (heap[0][0]-prev)*length <=k:    
        item,i=heapq.heappop(heap)
        sub_sum+=((item-prev) * length)
        prev=item
        length-=1
    
    heap.sort(key=lambda x: x[1])
    answer=heap[(k-sub_sum)%length][1]
    
    return answer

댓글남기기