-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path디스크 컨트롤러.py
52 lines (41 loc) · 1.36 KB
/
디스크 컨트롤러.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import heapq
def solution(jobs):
jobs.sort()
answer = 0
i = 0
h = []
n = len(jobs)
start_total = 0
end_total = 0
end = 0
now = 0
while jobs or h: # 남은 할일 or heap이 남아있다면
while jobs:
if now >= jobs[0][0]: # 제일 먼저 들어온 거부터 확인 -> now 이하이면 heap에 삽입
s, time = jobs.pop(0) # 들어온 시간, 소요시간
start_total += s
heapq.heappush(h, [time, s])
else:
break # 스케줄링 end타임
if h:
e, _ = heapq.heappop(h)
now += e # 실행 시간만큼 end가 바뀜
end_total += now
else:
now = jobs[0][0] # 비어있을 때?
answer = int((end_total - start_total) / n)
return answer
# start, end = -1, 0
# while i < len(jobs):
# for job in jobs[i:]:
# if start < job[0] <= end: # 아직 안끝났으면 -> heapq에 넣어줌
# heapq.heappush(h, [job[1], job[0]])
# if len(h) > 0:
# p = heapq.heappop(h)
# start = end
# end += p[0] # 실행 시간만큼 end가 늘어남
# answer += (end - p[1]) # 끝난 시간 - 시작시간
# i += 1
# else:
# end += 1
# return int(answer/len(jobs))