Skip to content

Commit 50de6cd

Browse files
committed
[PGS]#42627. 디스크컨트롤러/lv3/3hours
ttps://school.programmers.co.kr/learn/courses/30/lessons/42627
1 parent 045df3a commit 50de6cd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
# 우선순위
3+
- wait_que = [[작업 번호 , 작업 요청 시간 , 소요시간]]
4+
- if wait_que :
5+
#우선순위 높은 순 부터 작업 할당
6+
우선순위 : [소요시간 short , 요청시간이 fast , 번호가 작은 것]
7+
8+
- intercept 없음
9+
- 같은 time 에 HD에 작업이 끝나는 시점 == 다른 작업 요청이 들어오는 시점일 경우,
10+
HD 작업 종료 -> 바로 wait queue에서 ready queue로 할당
11+
- 출력 : 각 jobs들의 반환 시각의 평균값의 정수부분
12+
# INSPECT
13+
1. 다중 변수에 대한 우선순위 큐 사용하기
14+
2. time += 1 각 time 별로 (1) wait 큐에 <- jobs 할당하기 (2) HD 에 <- wait 큐속 작업 할당하기 등 전체 작업 진행
15+
"""
16+
import heapq
17+
def solution(jobs):
18+
answer = 0 ; a = []
19+
wait = []
20+
# 반복문 종료 조건 : 모든 jobs가 HD 작업 종료
21+
finished = 0 ; t = 0 ; hd_timeout = -1
22+
# time += 1 로 wait 큐와 HD 할당을 각 시각별로 확인 및 처리
23+
while finished < len(jobs) :
24+
# print(f"##t {t}")
25+
#[1] wait 큐에 요청 시각에 맞춰 작업 할당하기
26+
for idx , (accept_time , duration) in enumerate(jobs):
27+
if accept_time == t :
28+
heapq.heappush(wait , [duration , accept_time, idx])
29+
30+
#2. HD 작업 진행 여부 확인
31+
#[1]. HD 작업 진행중
32+
if hd_timeout > 0 : # HD 작업 진행중
33+
hd_timeout -=1
34+
#[2] HD 작업 완료 및 비어 있을 때(hd_timout 음수 , 초기 시점)
35+
elif hd_timeout <= 0 :
36+
if hd_timeout == 0 : # (1) 진행중인 HD 작업 완료 -> 완료 작업 개수 += 1
37+
a.append([t, start_t]) # 작업의 (종료 시점, 요청 시점) 을 정답 리스트에 반환
38+
finished += 1
39+
hd_timeout = -1 # hd 비어있음
40+
41+
# wait 큐의 존재하면 -> wait큐에서 우선순위 작업을 뽑아 HD에 할당하기
42+
if wait :
43+
hd_timeout , start_t , id = heapq.heappop(wait)
44+
hd_timeout = hd_timeout - 1
45+
46+
t += 1
47+
48+
# [4] 작업 반환 시간(turnaround_time) : 요청시간 - 완료 시간
49+
for e, s in a :
50+
answer += (e - s)
51+
answer = answer // len(a)
52+
53+
return answer

0 commit comments

Comments
 (0)