하나의 작업이 끝난 시점 전에 도착해있는 작업들을 소요 시간이 작은 순서대로 저장하기 위해 heapq를 사용하였다
heap이 비어있을 때와(작업이 도착하기 전) 비어있지 않을 때를 고려하여 코드를 작성해줘야 했다(어렵다...)
소스 코드
import heapq
def solution(jobs):
answer = 0
now, start = 0, -1
i = 0
heap = []
while i < len(jobs):
for j in jobs:
if start < j[0] <= now:
heapq.heappush(heap, [j[1], j[0]])
if len(heap) > 0:
current = heapq.heappop(heap)
start = now
now += current[0]
answer += (now - current[1])
i += 1
else:
now += 1
return int(answer // len(jobs))
def solution(jobs):
answer = 0
start = 0
jobs = sorted(jobs, key = lambda x:x[1])
length = len(jobs)
while len(jobs) != 0:
for i in range(len(jobs)):
if jobs[i][0] <= start:
start += jobs[i][1]
answer += start - jobs[i][0]
jobs.pop(i)
break
if i == len(jobs) - 1:
start += 1
return answer // length