본문 바로가기
Python/이것이 취업을 위한 코딩 테스트다(with 파이썬)

이것이 취업을 위한 코딩 테스트다 무지의 먹방 라이브

by sophia02 2022. 5. 29.
# 못풀어서 다시 풀어 볼 문제
import heapq


def solution(food_times, k):
    if sum(food_times) <= k:
        return -1
    q = []
    for i in range(len(food_times)):
        heapq.heappush(q, (food_times[i], i + 1))

    sum_value = 0
    previous = 0

    length = len(food_times)

    while sum_value + ((q[0][0] - previous) * length) <= k:
        now = heapq.heappop(q)[0]
        sum_value += (now-previous) * length
        length -= 1
        previous = now
    result = sorted(q, key=lambda x: x[1])
    return result[(k - sum_value) % length][1]