ISTP의 간단명료 블로그

[프로그래머스] 디스크 컨트롤러 테스트 케이스 / deque 본문

풀다

[프로그래머스] 디스크 컨트롤러 테스트 케이스 / deque

djwis 2023. 6. 23. 07:33

문제

풀이

from heapq import heapify, heappop, heappush
def solution(jobs):
    jobs.sort()
    answer = jobs[0][1]
    now = jobs[0][1]+jobs[0][0]
    waiting = []
            
    for x, y in jobs[1:]:
        if x < now:
            heappush(waiting, (y,x))
        elif waiting:
            while(x>now and waiting):
                b, a = heappop(waiting)      
                answer += now+b-a
                now = now+b
            heappush(waiting, (y,x))
        else:
            answer += y
            now = now+y
        print(answer)

    while(waiting):
        y, x = heappop(waiting)
        answer += now+y-x
        now = now+y
        
    return int(answer/len(jobs))

테스트 케이스 TEST CASE

다른사람 풀이

import heapq
from collections import deque

def solution(jobs):
    tasks = deque(sorted([(x[1], x[0]) for x in jobs], key=lambda x: (x[1], x[0])))
    q = []
    heapq.heappush(q, tasks.popleft())
    current_time, total_response_time = 0, 0
    while len(q) > 0:
        dur, arr = heapq.heappop(q)
        current_time = max(current_time + dur, arr + dur)
        total_response_time += current_time - arr
        while len(tasks) > 0 and tasks[0][1] <= current_time:
            heapq.heappush(q, tasks.popleft())
        if len(tasks) > 0 and len(q) == 0:
            heapq.heappush(q, tasks.popleft())
    return total_response_time // len(jobs)

배운점

  • deque