[Python] 디스크 컨트롤러

2669 단어 algorithmalgorithm

2 try 필수




1 Try

Failed

heapq.heapify(jobs)

ave = 0
total_len = len(jobs)

waiting = []

last = -1
end = 0

while jobs or waiting:
    
    if jobs: 
        start, duration = heapq.heappop(jobs)

        if last < start <= cur_time : heapq.heappush(waiting, (duration,start))  
    
    if cur_time < start or len(jobs) == 0 :
        # waiting 처리하기
        if waiting :
            heapq.heappush(jobs, [start,duration])
            d, s = heapq.heappop(waiting)
            last = cur_time
            cur_time += d
        else :
            cur_time = start + duration
        ave += cur_time - s

    print(f'2: wating : {waiting}, jobs : {jobs}, cur_time : {cur_time}, d :{duration} , s:{start}, ave : {ave}')

print(ave//total_len)

others

100/100

import heapq

def solution(jobs):
    count, last, answer = 0, -1, 0
    heap = []
    jobs.sort()
    # 시작시간 초기화
    time = jobs[0][0]
    while count < len(jobs):
        for s, t in jobs:
            if last < s <= time:
                # 작업 소요시간으로 min heap을 만들기 위해 (t, s) 푸시
                heapq.heappush(heap, (t, s))
        # 바로 수행할 수 있는 작업이 있는 경우
        if len(heap) > 0:
            count += 1
            last = time
            term, start = heapq.heappop(heap)
            time += term
            answer += (time - start)
        # 바로 수행할 수 있는 작업이 없는 경우
        else:
            time += 1
    return answer//len(jobs)
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)

좋은 웹페이지 즐겨찾기