Skip to content

Commit 6aa7cc6

Browse files
zaqquumMingguriguri
authored andcommitted
[PGS]#42627. 디스크컨트롤러/lv3/3hours
ttps://school.programmers.co.kr/learn/courses/30/lessons/42627
1 parent 7b70c9f commit 6aa7cc6

File tree

1 file changed

+52
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)