코딩테스트/프로그래머스

[프로그래머스][구현] 2018 카카오 [1차] 셔틀버스

박소민 2024. 6. 21. 10:39
[1차] 셔틀버스
 

프로그래머스

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

programmers.co.kr

 

  • 내 풀이
    • 구현으로 품
from bisect import bisect_left
def solution(n, t, m, timetable):
    answer = ''
    new_timetable=[]
    for time in sorted(timetable):
        hh,mm=map(int,time.split(':'))
        hh*=60
        new_timetable.append(hh+mm)
    
    current_time=540 #60*9 : 9시
    con_time=0
    arrived=0
    while n>0:
        n-=1
        idx=bisect_left(new_timetable, current_time)
        
        #마지막 운행일때
        if n==0: 
            #맨 마지막 사람보다 버스 시간이 뒤일 경우
            if idx>=len(new_timetable): 
                #사람이 남아있으면
                if arrived+m<=len(new_timetable):
                    #탑승가능한 마지막 사람보다 1분 빨리
                    con_time=new_timetable[arrived+m-1]-1       
                #사람이 남아있지 않으면 맨마지막에 콘이 타면 됨
                else:
                    con_time=current_time
                
            
            #마지막 시간이 무조건 전체 사람 이후(모두가 탈 수 있음)
            else:
                #시간 상 가능한 인원이 제한 인원 수보다 많을 때
                if idx>=arrived+m:
                    #제한 인원 맨 마지막 사람보다 1분빨리
                    con_time=new_timetable[arrived+m-1]-1

                #시간 상 가능한 인원이 제한 인원 수보다 적을 때
                else: # idx < arrived+m
                    if new_timetable[idx]==current_time: #딱 출발 시간이면 그보다 빨리
                        con_time= new_timetable[idx]-1
                    else: #idx 위치값이 출발시간 보다 뒷타임이면 => 자리 비어있음
                        con_time=current_time
                        
            arrived=min(idx,arrived+m)
            break

        #아직 운행이 남아있을 때
        else:
            #버스 시간과 같은 시간의 사람 수
            plus=new_timetable[idx:].count(current_time)
            #버스 시간이 탑승 가능한 맨마지막 사람보다 앞일 때(인원은 되는데 시간이 늦음)
            if idx+plus<arrived+m-1:
                arrived=idx+plus #탄 사람만
            else:
                arrived+=m #다음 가능한 수
            current_time+=t
    
    con_h=con_time//60
    con_m=con_time%60
    answer=answer="{:02d}:{:02d}".format(con_h, con_m)
    return answer

 

 

  • 다른 사람 풀이
from collections import deque


def convert(timeString):
    h, m = map(int, timeString.split(":"))
    return h * 60 + m


def solution(n, t, m, timetable):
    timetable = sorted(list(map(convert, timetable)))
    que = deque(timetable)

    answer = 0
    currTime = 9 * 60
    for _ in range(n):
        crewCount = 0
        last = -1
        while crewCount < m and que and que[0] <= currTime:
            last = que.popleft()
            crewCount += 1

        if crewCount < m:
            answer = currTime
        else:
            answer = last - 1

        currTime += t

    answer = str(answer // 60).zfill(2) + ":" + str(answer % 60).zfill(2)
    return answer