[프로그래머스] 셔틀버스 (python 파이썬)

코딩하는계란·2021년 6월 21일
0

프로그래머스

목록 보기
13/16
post-thumbnail

👉 셔틀버스



✍ 내 코드


# 2018 KAKAO BLIND RECRUITMENT  [1차] 셔틀버스

from datetime import datetime, timedelta


def solution(n, t, m, timetable):
    time = [datetime.strptime(i, "%H:%M") for i in timetable]
    time.sort()
    idx = 0
    result = datetime.strptime("00:00", "%H:%M")
    start = datetime.strptime("09:00", "%H:%M")
    for i in range(n):
        now = start + timedelta(minutes=t * i)
        cnt = 0
        while idx < len(time) and time[idx] <= now and cnt < m:
            idx += 1
            cnt += 1

        if cnt < m:
            result = now
        else:
            result = time[idx - 1] - timedelta(minutes=1)

    return result.strftime("%H:%M")


✍ 팁


  • 시간형태로 모두 변환한뒤 정렬해준다
  • 버스가 다 찬경우와 비어있는 경우를 따로 계산한다 (다 차있는경우는 가능했던 time - 1분 한 값을 넣는다)

👉 잡담


풀이들을 많이 구경해봤는데 이렇게 시간으로 변환해 푸는경우는 못봤다.
나름 괜찮은 풀이라고 생각한다 ^ㅡ^

profile
코딩💻 고양이😺

0개의 댓글