몸과 마음이 건전한 SW 개발자

프로그래머스 [Lv. 3] [1차] 셔틀버스 {언어 : Python} [다시 풀어 보기] 본문

알고리즘/다시 풀어 보기

프로그래머스 [Lv. 3] [1차] 셔틀버스 {언어 : Python} [다시 풀어 보기]

스위태니 2024. 6. 28. 01:50

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/17678

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

정답 코드

def solution(n, t, m, timetable):
    # 셔틀 도착 시각 리스트 (분 단위로 변환)
    busStopTimetable = [540 + i*t for i in range(n)]
    last = n - 1
    # 크루 도착 시각 리스트 (분 단위로 변환)
    sortedTimetable = sorted([int(time[:2]) * 60 + int(time[3:]) for time in timetable])
    
    crewIdx = 0
    for j in range(n):
        currentStopTime = busStopTimetable[j]
        # 현재 버스에 탈 수 있는 크루 수 계산
        count = 0
        while crewIdx < len(sortedTimetable) and sortedTimetable[crewIdx] <= currentStopTime and count < m:
            crewIdx += 1
            count += 1
        
        # 마지막 버스일 경우
        if j == last:
            if count < m:
                # 버스에 자리 남으면 버스 도착 시간에 맞춰서 탑승
                answer = currentStopTime
            else:
                # 마지막으로 탄 크루보다 1분 일찍 도착해야 함
                answer = sortedTimetable[crewIdx - 1] - 1

    # 정답을 HH:MM 형식으로 변환
    hours = answer // 60
    minutes = answer % 60
    return f"{hours:02d}:{minutes:02d}"

풀이 방법

  • 주석과 함께 풀면 끝!

느낀점

  • 쉽게 풀지 못했다.
  • 다시 풀면 좋을 것 같다.