Git: https://github.com/Tarte12/CodingTest_KUT/commit/e52238192d7938363f1b5ff6e04e2e6b9c8211a7
import heapq
import sys
input = sys.stdin.readline
n = int(input())
times = [tuple(map(int, input().split())) for _ in range(n)]
#times = [(s1, t1), (s2, t2), (s3, t3)...]
answer = 0
times.sort()
heap = [] #진행 중 강의들의 종료 시간
for s, e in times: #times는 배정되어야 하므로 다 돌아야 해
while heap and heap[0] <= s:
heapq.heappop(heap)
heapq.heappush(heap, e)
answer = max(answer, len(heap))
print(answer)
heap[0] <= s 것들을 모두 pop (방 재사용 가능)len(heap)이 최댓값len(heap) = 현재 필요한 방 수| 문제 | 질문 | 전략 | 자료구조 |
|---|---|---|---|
| 회의실 배정(1931) | 한 방에 겹치지 않게 최대 몇 개 회의를 고를까? | 끝나는 시간 오름차순 그리디 선택 | 불필요 |
| 강의실 배정(11000) | 최소 몇 개 방이 있어야 전부 배정 가능한가? | 시작 시간 오름차순 + 종료시각 min-heap | 필수 (heap) |
heapq는 min-heap만 제공 → 가장 작은 값이 루트 heap[0]-x) 또는 (우선순위, 데이터) 튜플에서 우선순위를 음수로heap[0] <= s이면 가장 빨리 끝난 것조차 s 이전/동시에 끝난 것이므로 pop