본문 바로가기

코딩 문제/프로그래머스

[프로그래머스] 디스크 컨트롤러 (python)

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

 

코딩테스트 연습 - 디스크 컨트롤러

하드디스크는 한 번에 하나의 작업만 수행할 수 있습니다. 디스크 컨트롤러를 구현하는 방법은 여러 가지가 있습니다. 가장 일반적인 방법은 요청이 들어온 순서대로 처리하는 것입니다. 예를

programmers.co.kr

내 풀이

import heapq

def solution(jobs):
    answer, now, i = 0,0,0
    start = -1
    heap= []

    while i < len(jobs):
        for j in jobs:
            if start < j[0] <= now:
                heapq.heappush(heap, [j[1], j[0]])
        if len(heap) > 0:
            current = heapq.heappop(heap)
            start = now
            now += current[0]
            answer += (now-current[1])
            i += 1
        else:
            now += 1
    return int(answer / len(jobs))