L3 : 디스크 컨트롤러 Python

jhyunn·2023년 1월 18일
0

Programmers

목록 보기
34/69

L3 : 디스크 컨트롤러 Python

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

import heapq
from collections import deque

def solution(jobs):
    n = len(jobs)
    jobs = deque(sorted(jobs)) # 시작 시간 기준으로 정렬
    wait = []
    cur_time, work_time = 0, 0

    while len(jobs) != 0 or len(wait) != 0:
        while jobs:
            if jobs[0][0] <= cur_time:
                job = jobs.popleft()
                heapq.heappush(wait, [job[1], job[0]]) # heapq 정렬을 위해 job을 뒤집음
            elif len(wait) == 0:
                cur_time = jobs[0][0]
            else:
                break

        cur_job = heapq.heappop(wait)
        cur_time += cur_job[0]
        work_time += cur_time - cur_job[1]
            
    return work_time//n

cur_time(현재 시간)이 다음 job의 시작 시간보다 크다면, 대기 중인 job이 모여있는 work에 cur_time보다 작은 모든 job을 추가한다.

대기 중인 job들(=work)은 소요 시간이 작은 순으로 정렬이 되도록 heappush()한다.

#heapq #sort

profile
https://github.com/Sungjeonghyun

0개의 댓글