(Python) 프로그래머스 - [1차] 셔틀버스

[문제 링크]

풀이

import heapq

def solution(n, t, m, timetable):
    seconds_store = []
    for time in timetable:
        seconds_store.append(time_to_min(time))
    heapq.heapify(seconds_store)
    bus_start = 9 * 60

    cnt = 0
    loop = 1
    while seconds_store:

        crew = heapq.heappop(seconds_store)

        # 막차인데 인원 수만큼 대기중인 경우
        if loop == n and cnt == m - 1:
            # crew가 차 시간보다 같거나 작은 경우 해당 crew보다 1분 먼저 도착
            if crew <= bus_start:
                return min_to_time(crew - 1)
            # crew가 차 시간보다 늦게 도착하는 경우 해당 차의 시간을 리턴
            else:
                return min_to_time(bus_start)

        if crew <= bus_start:
            cnt += 1
            # 꽉차면 다음 배차
            if cnt == m:
                bus_start += t
                cnt = 0
                loop += 1

        # 다음 배차로 넘어가야 한다.
        else:
            cnt = 0
            loop += 1
            bus_start += t
            heapq.heappush(seconds_store, crew)

    # 자리가 널널한 경우
    last_bus_min = 9 * 60 + t * (n - 1)
    return min_to_time(last_bus_min)


def time_to_min(time):
    split = list(map(int, time.split(":")))
    return split[0] * 60 + split[1]


def min_to_time(min):
    tmp_hour = str(min // 60)
    tmp_min = str(min % 60)
    if len(tmp_hour) == 1:
        tmp_hour = "0" + tmp_hour
    if len(tmp_min) == 1:
        tmp_min = "0" + tmp_min
    return tmp_hour + ":" + tmp_min


print(solution(1, 1, 1, ["23:59"]))

© 2021. By Backtony