알고리즘

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

빅디 2024. 5. 8. 01:26
728x90

😉 아이디어

1. 과제가 진행되는 시간 기준 구현 _!0초 ~ plans의 최대값 * 1_000!_

2. 매 초마다 _!now!_에 저장된 과제의 처리시간 감소

3. 과제를 멈추고 새로운 과제를 시작할 경우 _!stopped!_리스트에 값 추가

    👉 과제가 종료되면 이 리스트에서 꺼내 할당

😉 풀이

from collections import deque

def solution(plans):
    for plan in plans:
        plan[1] = hm_to_ss(plan[1])
        plan[2] = mm_to_ss(plan[2])
        plan.append(plan[1] + plan[2])
        
    plans.sort(key = lambda x : x[1])
    # print('plans', plans)
    
    MIN_TIME = plans[0][1]
    MAX_TIME = 1_000 * mm_to_ss('100')
    pdq = deque(plans)
    now = []
    cleared = []
    stopped = deque()
    
    for t in range(MIN_TIME, MAX_TIME):
        if len(now) > 0 and now[2] == 0:
            # print('t = ' + str(t) + ', 남은 시간 = ' + str(now[2]))
            cleared.append(now[0])
            # 진행 중이던 과제가 끝냈을 때, 새로운 과제가 있다면 과제 이어서 진행
            if len(stopped) > 0:
                now = stopped.pop()
        
        # 기존 과제를 멈추고 무조건 새로운 과제 시작
        if len(pdq) > 0 and t == pdq[0][1]:
            if len(now) > 0:
                stopped.append(now)
            now = pdq.popleft()
        
        # 과제 시간 감소
        if len(now) > 0:
            now[2] -= 1
    
    return cleared

def hm_to_ss(time):
    t = time.split(':')
    return int(t[0]) * 3_600 + int(t[1]) * 60

def mm_to_ss(time):
    return int(time) * 60

✔ 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/176962?language=python3