본문 바로가기

컴퓨터/코테

프로그래머스 - 디스크 컨트롤러[heap]

import heapq

def solution(jobs):
    jobLen = len(jobs)
    jobs.sort()
    
    waitSum = 0
    time = jobs[0][0]
    
    for comeIn, jobTime in jobs:
        if comeIn > time:
            time = comeIn + jobTime
        else:
            time += jobTime
        waitSum += time - comeIn
    answer1 = waitSum // jobLen

    waitSum = 0
    time = jobs[0][0]
    heap = []
    task = jobs.pop(0)
    heapq.heappush(heap, (task[1], task))

    while jobs or heap:
        if heap:
            task = heapq.heappop(heap)[1]
            if task[0] > time :
                time = task[0] + task[1]
            else :
                time += task[1]
            waitSum += time - task[0]
            while jobs and jobs[0][0] <= time:
                task = jobs.pop(0)
                heapq.heappush(heap, (task[1], task))

        else:
            task = jobs.pop(0)
            heapq.heappush(heap, (task[1], task))

    answer2 = waitSum // jobLen
    return answer1 if answer1 < answer2 else answer2


print(solution(	[[24, 10], [28, 39], [43, 20], [37, 5], [47, 22], [20, 47], [15, 34], [15, 2], [35, 43], [26, 1]]), 72)

heapq 라이브러리를 사용하니 구현문제가 되어버린....