solution
1. 시작 가능한 시간에 시작하는 모든 회의들 중 끝나는 시간이 가장 빠른 것부터 처리 (우선순위큐 이용)
(1, 4)회의가 끝난 후 시작 가능한 회의는 (5, 7), (5, 9), (6, 10), (8, 11), (8, 12), (12, 14) 중 (5, 7)이 가장 빨리 끝남
2. (0, 6)은 4시에 회의가 끝나는데 시작이 0시이므로 삭제 (while문 이용해서 계속 pop)
while heap and heap[0][1] < tmp[0] : heap에 원소가 없으면 heap[0][1] 접근 불가 heap and 가 먼저 와야 함
heap[0][1] < tmp[0] and heap : 안 됨. 원소가 있는지부터 확인해야 함 (런타임에러)
# 1931, 회의실배정
import sys
import heapq
n = int(sys.stdin.readline())
heap = []
for _ in range(n):
start, end = map(int, sys.stdin.readline().split())
heapq.heappush(heap, (end, start))
cnt = 0
# 끝, 시작
while heap:
tmp = heapq.heappop(heap)
cnt += 1
if not heap:
break
while heap and heap[0][1] < tmp[0]:
heapq.heappop(heap)
print(cnt)
문제 출처 www.acmicpc.net/problem/1931
1931번: 회의실배정
(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.
www.acmicpc.net
'알고리즘 > 우선순위 큐' 카테고리의 다른 글
자바 | 백준 | 17612 | 쇼핑몰 (0) | 2021.05.01 |
---|---|
파이썬 | 백준 | 1715 | 카드 정렬하기 (0) | 2020.09.23 |