title: "[프로그래머스] [1차] 셔틀버스 Python 파이썬 해설 (Level 3) - 이도훈"
cleanUrl: "programmers/17678"
description: "프로그래머스 Level 3 문제 [[1차] 셔틀버스]의 풀이를 정리합니다."

문제 설명 및 제한사항

아이디어 및 해결 방법

코드

from collections import deque

BUSSTART = 540
CREW, BUS = 0, 1

def h2m(t):
    return 60*int(t.split(':')[0]) + int(t.split(':')[1])

def m2h(t):
    return f'{t//60:02}:{t%60:02}'

def solution(n, t, m, timetable):
    crewtimes = [(h2m(t), CREW) for t in timetable]
    bustimes = [(BUSSTART + t*i, BUS) for i in range(n)]
    
    timepoints = crewtimes + bustimes
    timepoints.sort()
    
    q, groups = deque(), []
    for t, typ in timepoints:
        if typ == CREW:
            q.append(t)
        else:
            group = tuple(q.popleft() for _ in range(min(m, len(q))))
            groups.append((group, t))
    
    if len(groups[-1][0]) < m:
        return m2h(groups[-1][1])
    # 막차에 꽉 찼을 경우입니다.
    else:
        # 막차에 가장 늦게 탈 수 있는 경우를 찾습니다.
        last, i = groups[-1][0][-1], m-1
        while i > 0:
            if groups[-1][0][i-1] != last:
                break
            i -= 1
        return m2h(groups[-1][0][i] - 1)
        
        
    
    
    answer = ''
    return answer

출처

프로그래머스 코딩테스트 연습 https://school.programmers.co.kr/learn/challenges