문제 링크

풀이 과정

전체 코드

def strToInt(time):
        return int(time[:2]) * 60 + int(time[3:])
    
def solution(plans):
    answer = []
    stack = []
    plans = sorted(
        list(map(lambda x: [x[0], strToInt(x[1]), int(x[2])], plans)),
        key=lambda y: y[1]
    )
    
    cur_time = plans[0][1]  # 현재 시간
    for name, start, playtime in plans:
        # 현재 시간이 새로운 과제 시작 시간 이후일 때 -> 실제로는 이전 과제를 다 끝마친 것이 아님
        if cur_time > start:   
            time_left = cur_time - start
            stack.append((answer.pop(), time_left))  # (과제 이름, 잔여 시간)
            cur_time = start            
        
        # 현재 시간이 새로운 과제 시작 시간 이전인 경우 -> 기존 잔여 과제를 수행할 수 있음
        elif cur_time < start:   
            # 새로운 과제 시작까지 남은 시간
            time_left = start - cur_time
            # 남는 시간만큼 잔여 과제를 수행(1개 이상)
            while stack:
                cur_name, time = stack.pop()
                if time_left >= time:
                    answer.append(cur_name)
                    time_left -= time
                else:
                    stack.append((cur_name, time-time_left))
                    break
            cur_time = start
                      
        # 기본적으로 현재 과제를 완전히 수행했다고 가정함.
        cur_time += playtime
        answer.append(name)
        
    while stack:
        answer.append(stack.pop()[0])
            
    return answer