Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
프로그래머스. 이중 우선순위 큐
https://school.programmers.co.kr/learn/courses/30/lessons/42628
유형: Priority Queue
"""
16 changes: 16 additions & 0 deletions _WeeklyChallenges/W27-[PriorityQueue]/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## 🚀7월 1주차 (6/30) 스터디 발제 주제: Priority Queue
> 발제자: 김민정 (@Mingguriguri)

> [!NOTE]
> 주제: Priority Queue

### 🗂️ 스터디 자료
- PDF: [바로가기](Study_PGS_42627.pdf)

### 📖 문제
- [프로그래머스 #디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627?language=python3): 우선순위 큐 / Level 3
- 정답 코드: [Study_PGS_42627_디스크컨트롤러.py](Study_PGS_42627_디스크컨트롤러.py)

### 💻 과제
- [프로그래머스 #이중우선순위큐](https://school.programmers.co.kr/learn/courses/30/lessons/42628): 우선순위 큐 / Level 3
- 정답 코드: [Assignment_PGS_42628_이중우선순위큐.py](Assignment_PGS_42628_이중우선순위큐.py)
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
프로그래머스. 디스크 컨트롤러
https://school.programmers.co.kr/learn/courses/30/lessons/42627
유형: Priority Queue
"""
"""
풀이1
"""
import heapq

def solution(jobs):
jobs.sort() # 요청시간 기준 정렬
job_len = len(jobs)
i = 0 # jobs 인덱스
end_time = 0 # 현재 시간
return_time = 0 # 작업 반환 시간
count = 0 # 작업 처리한 개수

heap = []

while count < job_len:
# 현재 시간에 요청된 작업 처리
while i < job_len and jobs[i][0] <= end_time:
heapq.heappush(heap, (jobs[i][1], jobs[i][0], i)) # 소요시간, 요청시간, 작업번호 순서
i += 1

# 대기 큐에 작업이 있다면, 시간을 업데이트한다.
if len(heap) > 0:
work_time, start_time, num = heapq.heappop(heap)
end_time += work_time
return_time += end_time - start_time
count += 1
else:
# 대기 큐가 비었다면, 다음 작업이 올 때까지 기다려야 한다.
end_time = jobs[i][0]

return return_time // job_len


"""
풀이2
출처: 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
"""
import heapq


def solution(jobs):
answer = 0
now = 0 # 현재시간
i = 0 # 처리개수
start = -1 # 마지막 완료시간
heap = []

while i < len(jobs):
for job in jobs:
if start < job[0] <= now:
heapq.heappush(heap, [job[1], job[0]])

if heap:
current = heapq.heappop(heap)
start = now
now += current[0]
answer += now - current[1] # 요청으로부터 처리시간
i += 1
else:
now += 1

return answer // len(jobs)

Loading