diff --git "a/_WeeklyChallenges/W27-[PriorityQueue]/Assignment_PGS_42628_\341\204\213\341\205\265\341\204\214\341\205\256\341\206\274\341\204\213\341\205\256\341\204\211\341\205\245\341\206\253\341\204\211\341\205\256\341\206\253\341\204\213\341\205\261\341\204\217\341\205\262.py" "b/_WeeklyChallenges/W27-[PriorityQueue]/Assignment_PGS_42628_\341\204\213\341\205\265\341\204\214\341\205\256\341\206\274\341\204\213\341\205\256\341\204\211\341\205\245\341\206\253\341\204\211\341\205\256\341\206\253\341\204\213\341\205\261\341\204\217\341\205\262.py" new file mode 100644 index 0000000..b6bdd91 --- /dev/null +++ "b/_WeeklyChallenges/W27-[PriorityQueue]/Assignment_PGS_42628_\341\204\213\341\205\265\341\204\214\341\205\256\341\206\274\341\204\213\341\205\256\341\204\211\341\205\245\341\206\253\341\204\211\341\205\256\341\206\253\341\204\213\341\205\261\341\204\217\341\205\262.py" @@ -0,0 +1,5 @@ +""" +프로그래머스. 이중 우선순위 큐 +https://school.programmers.co.kr/learn/courses/30/lessons/42628 +유형: Priority Queue +""" diff --git a/_WeeklyChallenges/W27-[PriorityQueue]/README.md b/_WeeklyChallenges/W27-[PriorityQueue]/README.md new file mode 100644 index 0000000..9a210d2 --- /dev/null +++ b/_WeeklyChallenges/W27-[PriorityQueue]/README.md @@ -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) diff --git a/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627.pdf b/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627.pdf new file mode 100644 index 0000000..2a77cd2 Binary files /dev/null and b/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627.pdf differ diff --git "a/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627_\341\204\203\341\205\265\341\204\211\341\205\263\341\204\217\341\205\263\341\204\217\341\205\245\341\206\253\341\204\220\341\205\263\341\204\205\341\205\251\341\206\257\341\204\205\341\205\245.py" "b/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627_\341\204\203\341\205\265\341\204\211\341\205\263\341\204\217\341\205\263\341\204\217\341\205\245\341\206\253\341\204\220\341\205\263\341\204\205\341\205\251\341\206\257\341\204\205\341\205\245.py" new file mode 100644 index 0000000..9fde352 --- /dev/null +++ "b/_WeeklyChallenges/W27-[PriorityQueue]/Study_PGS_42627_\341\204\203\341\205\265\341\204\211\341\205\263\341\204\217\341\205\263\341\204\217\341\205\245\341\206\253\341\204\220\341\205\263\341\204\205\341\205\251\341\206\257\341\204\205\341\205\245.py" @@ -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) +