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