문제
https://school.programmers.co.kr/learn/courses/30/lessons/42627
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
풀이
우선 순위대로 문제를 처리하므로 힙을 써야하는 것은 명확하다. 하지만, 이 힙을 어떻게 다루느냐가 더 중요한 문제이기 때문에 사실상 힙보다는 단순 구현 문제에 가깝다.
코드
import heapq
def solution(jobs):
jobs.sort(key = lambda x: x[0])
waitQueue = []
finished = 0
currentTime = 0
jobIdx = 0
totalReturnTime = 0
while finished < len(jobs):
while jobIdx < len(jobs) and jobs[jobIdx][0] <= currentTime:
requestTime, duration = jobs[jobIdx]
heapq.heappush(waitQueue, (duration, requestTime))
jobIdx += 1
if waitQueue:
duration, requestTime = heapq.heappop(waitQueue)
currentTime += duration
returnTime = currentTime - requestTime
totalReturnTime += returnTime
finished += 1
else:
currentTime = jobs[jobIdx][0]
return totalReturnTime // len(jobs)
'Online Judge > Problem Solving' 카테고리의 다른 글
| [프로그래머스] 셔틀버스 - Swift (0) | 2025.08.03 |
|---|---|
| [프로그래머스] 거스름돈 - Python (0) | 2025.08.03 |
| [프로그래머스] 징검다리 건너기 - Swift (0) | 2025.07.22 |
| [프로그래머스] 불량 사용자 - Python (0) | 2025.07.21 |
| [프로그래머스] 단속 카메라 - Python (0) | 2025.07.21 |