Link: https://programmers.co.kr/learn/courses/30/lessons/42627
jobs return
[[0, 3], [1, 9], [2, 6]] 9
import heapq
def solution(jobs):
sec = -1
answer = 0
heapq.heapify(jobs)
inProcess = []
l = len(jobs)
working = False
while True:
#loop out
if not jobs and not inProcess and not working : break
sec += 1
# at some point(sec), push over to heap list from jobs
while jobs:
if jobs[0][0] == sec:
heapq.heappush(inProcess,[jobs[0][1],jobs[0][0]])
heapq.heappop(jobs)
else: break
if not jobs: break
#check if the machine is working or not
if not working: #if not, then start working
if not inProcess: continue
p_cnt = inProcess[0][0] - 1
p_start = inProcess[0][1] -1
heapq.heappop(inProcess)
working = True
else: p_cnt -= 1 #if working, then keep working
#check if working is done. if yes, +answer then, make 'working' back to False
if p_cnt == 0:
answer += sec - p_start
working = False
return answer//l
#1 Input: solution([[0, 3], [10, 3], [2, 4]])
#1 Output: 3
#2 Input: solution([[0, 1], [1, 1], [3, 1], [2,6],[50, 7]])
#2 Output: 4
import heapq
def solution(jobs):
answer, ms = 0, -1
length = len(jobs)
working = False
requested = []
heapq.heapify(jobs)
end = -1
while True:
if not jobs and not requested and not working: break
ms += 1
#각 해당 시간에 할당 가능 job을 requested에 할당(heappush)
while jobs:
if jobs[0][0] == ms:
heapq.heappush(requested,[jobs[0][1],jobs[0][0]])
heapq.heappop(jobs)
else: break
#일하고 있는 중인지, 일이 완료되는 시점인지 확인
if working and ms == end:
working = False
#일하지 않는 중이면, 일 할당
if not working:
if not requested: continue
temp = heapq.heappop(requested)
end = ms + temp[0]
start = temp[1]
answer += end - start
working = True
return answer//length
print(solution([[0, 3], [1, 9], [2, 6]]))