[프로그래머스] 과제 진행하기

박형진·2023년 4월 6일
0

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


1. 코드

def solution(plans):
    ans = []
    stack = []

    for i in range(len(plans)):
        plans[i][1], plans[i][2] = (int(plans[i][1][:2]) * 60) + int(plans[i][1][3:]), int(plans[i][2])
    plans.sort(key=lambda x: x[1])

    for i in range(1, len(plans)):
        time = plans[i][1] - plans[i-1][1]
        if time>=plans[i-1][2]:
            ans.append(plans[i-1][0])
            remain_time = time - plans[i-1][2]
			# if문이 아닌 while 문을 사용하여 remain_time 을 모두 소모해야 한다.
            while stack and remain_time >= 0:
                name, cost = stack.pop()
                if remain_time < cost:
                    stack.append((name, cost-remain_time))
                else:
                    ans.append(name)
                remain_time -= cost
        else:
            plans[i-1][2] -= time
            stack.append((plans[i-1][0], plans[i-1][2]))

    ans.append(plans[-1][0])
    while stack:
        name, _ = stack.pop()
        ans.append(name)
    return ans
    
profile
안녕하세요!

0개의 댓글