목차

디스크 컨트롤러

ps
링크programmers.co.kr/…
출처프로그래머스
문제 번호42627
문제명디스크 컨트롤러
레벨Level 3
분류

그리디

시간복잡도O(nlogn)
인풋사이즈n<=500
사용한 언어Python
해결날짜2021/05/28
태그

고득점 Kit - 힙(Heap)

풀이

코드

"""Solution code for "Programmers 42627. 디스크 컨트롤러".

- Problem link: https://programmers.co.kr/learn/courses/30/lessons/42627
- Solution link: http://www.teferi.net/ps/problems/programmers/42627
"""

import heapq


def solution(jobs):
    total_time = 0
    current_time = 0        
    job_count = len(jobs)
    jobs.sort(reverse=True)
    job_heap = []    
    for _ in range(job_count):
        while jobs:
            requested_time, execution_time = jobs[-1]
            if requested_time > current_time:
                break
            heapq.heappush(job_heap, (execution_time, requested_time))
            jobs.pop()
        if job_heap:
            execution_time, requested_time = heapq.heappop(job_heap)
            current_time += execution_time
        else:
            requested_time, execution_time = jobs.pop()
            current_time = requested_time + execution_time
        total_time += current_time - requested_time
    
    return total_time // job_count