Skip to content

Commit 57ed954

Browse files
authored
feat: 발제 문제 풀이2번 수정
1 parent 5e8c5ad commit 57ed954

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627_디스크컨트롤러.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,27 @@ def solution(jobs):
4343
"""
4444
import heapq
4545

46-
def solution(jobs):
47-
jobs.sort() # 요청시간 기준 정렬
48-
job_len = len(jobs)
49-
i = 0 # jobs 인덱스
50-
end_time = 0 # 현재 시간
51-
return_time = 0 # 작업 반환 시간
52-
count = 0 # 작업 처리한 개수
5346

47+
def solution(jobs):
48+
answer = 0
49+
now = 0 # 현재시간
50+
i = 0 # 처리개수
51+
start = -1 # 마지막 완료시간
5452
heap = []
5553

56-
while count < job_len:
57-
# 현재 시간에 요청된 작업 처리
58-
while i < job_len and jobs[i][0] <= end_time:
59-
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
60-
i += 1
54+
while i < len(jobs):
55+
for job in jobs:
56+
if start < job[0] <= now:
57+
heapq.heappush(heap, [job[1], job[0]])
6158

62-
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
63-
if len(heap) > 0:
64-
work_time, start_time, num = heapq.heappop(heap)
65-
end_time += work_time
66-
return_time += end_time - start_time
67-
count += 1
59+
if heap:
60+
current = heapq.heappop(heap)
61+
start = now
62+
now += current[0]
63+
answer += now - current[1] # 요청으로부터 처리시간
64+
i += 1
6865
else:
69-
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
70-
end_time = jobs[i][0]
66+
now += 1
67+
68+
return answer // len(jobs)
7169

72-
return return_time // job_len

0 commit comments

Comments
 (0)