ps | |
---|---|
링크 | programmers.co.kr/… |
출처 | 프로그래머스 |
문제 번호 | 42627 |
문제명 | 디스크 컨트롤러 |
레벨 | Level 3 |
분류 |
그리디 |
시간복잡도 | O(nlogn) |
인풋사이즈 | n<=500 |
사용한 언어 | Python |
해결날짜 | 2021/05/28 |
태그 |
"""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