목차

무지의 먹방 라이브

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호42891
문제명무지의 먹방 라이브
레벨Level 4
분류

정렬

시간복잡도O(nlogn)
인풋사이즈n <= 200,000
사용한 언어Python
해결날짜2020/12/21

풀이

코드

"""Solution code for "Programmers 42891. 무지의 먹방 라이브".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/42891
- Solution link: http://www.teferi.net/ps/problems/programmers/42891
"""


def solution(food_times, k):
    answer = -1
    food_count = len(food_times)
    time_and_nums = sorted((t, i) for i, t in enumerate(food_times))    
    is_remaining = [True] * food_count
    remaining_food_count = food_count
    remaining_time = k
    prev_time = 0
    for cur_time, num in time_and_nums:
        spent_time = remaining_food_count * (cur_time - prev_time)
        if spent_time > remaining_time:
            remaining_foods = [i for i in range(food_count) if is_remaining[i]]
            answer = remaining_foods[remaining_time % remaining_food_count] + 1
            break
        is_remaining[num] = False
        remaining_food_count -= 1
        remaining_time -= spent_time
        prev_time = cur_time

    return answer