Skip to content

Commit 5e8c5ad

Browse files
authored
feat: 7월 1주차 스터디 발제 자료 및 코드 업로드
1 parent 5468b41 commit 5e8c5ad

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
프로그래머스. 이중 우선순위 큐
3+
https://school.programmers.co.kr/learn/courses/30/lessons/42628
4+
유형: Priority Queue
5+
"""
793 KB
Binary file not shown.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
프로그래머스. 디스크 컨트롤러
3+
https://school.programmers.co.kr/learn/courses/30/lessons/42627
4+
유형: Priority Queue
5+
"""
6+
"""
7+
풀이1
8+
"""
9+
import heapq
10+
11+
def solution(jobs):
12+
jobs.sort() # 요청시간 기준 정렬
13+
job_len = len(jobs)
14+
i = 0 # jobs 인덱스
15+
end_time = 0 # 현재 시간
16+
return_time = 0 # 작업 반환 시간
17+
count = 0 # 작업 처리한 개수
18+
19+
heap = []
20+
21+
while count < job_len:
22+
# 현재 시간에 요청된 작업 처리
23+
while i < job_len and jobs[i][0] <= end_time:
24+
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
25+
i += 1
26+
27+
# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
28+
if len(heap) > 0:
29+
work_time, start_time, num = heapq.heappop(heap)
30+
end_time += work_time
31+
return_time += end_time - start_time
32+
count += 1
33+
else:
34+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
35+
end_time = jobs[i][0]
36+
37+
return return_time // job_len
38+
39+
40+
"""
41+
풀이2
42+
출처: https://velog.io/@kiwoong96/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4PythonLevel3-%EB%94%94%EC%8A%A4%ED%81%AC-%EC%BB%A8%ED%8A%B8%EB%A1%A4%EB%9F%AC
43+
"""
44+
import heapq
45+
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 # 작업 처리한 개수
53+
54+
heap = []
55+
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
61+
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
68+
else:
69+
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
70+
end_time = jobs[i][0]
71+
72+
return return_time // job_len

0 commit comments

Comments
 (0)