본문 바로가기
알고리즘/프로그래머스

[Python] 프로그래머스 셔틀버스

by 컴공맨 2021. 5. 16.
 

코딩테스트 연습 - [1차] 셔틀버스

10 60 45 ["23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"] "18:00"

programmers.co.kr


풀이

문제에서 주어진 조건대로 마지막 셔틀버스까지 왔을 때, 콘이 버스를 탈 수 있다면 현재 버스시간을 그대로 두고 만약 콘이 버스를 탈 수 없다면 마지막으로 탄 사람의 시간에서 1분을 빼서 콘이 먼저 탈 수 있게 하여 해결했습니다.


코드

def solution(n, t, m, timetable):
    answer = ''

    timetable.sort()
    timetable_min = [int(time[:2]) * 60 + int(time[3:5]) for time in timetable]

    bus_time = 9 * 60
    idx = 0

    for bus_cnt in range(n):
        cur_crew_cnt = 0
        for crew_cnt in range(idx, len(timetable_min)):
            if timetable_min[idx] <= bus_time:
                idx += 1
                cur_crew_cnt += 1

            if cur_crew_cnt == m:
                break

        if bus_cnt == n - 1:
            if cur_crew_cnt == m:
                bus_time = timetable_min[idx - 1] - 1
            break

        bus_time += t

    answer = '%02d:%02d' % (bus_time // 60, bus_time % 60)
    return answer

 

pyo7410/Algorithm

1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.

github.com