알고리즘 문제풀이

최소 회의실 개수 [백준 19598]

wiojfe 2024. 10. 3. 09:46

https://www.acmicpc.net/problem/19598

 

heapq를 이용해서 진행되고 있는 강의 중에서 제일 먼저 종료되는 강의들을 찾아내면 된다. 

import heapq 
n = int(input()); total = 1; lastend=0 
heap =[]; arr =[] 
for i in range(n):
    a,b = map(int, input().split())
    arr.append((a,b))
arr.sort() 
heapq.heapify(heap)
heapq.heappush(heap, arr[0][1]) 
for start,end in arr[1:] :
    lastend = heapq.heappop(heap) 
    if lastend<= start :
        # lastend = end 
        total+=0 
    else:
        heapq.heappush(heap,lastend)
        total+=1
    heapq.heappush(heap, end) 
print(total)