[BOJ] 11000 강의실 배정 (Python)

11000번: 강의실 배정

최소힙 자료구조를 사용한다.

시작시간과 종료시간들간의 관계를 생각하면 됨

import sys
import heapq

input = sys.stdin.readline
n = int(input())
times = [list(map(int,input().split())) for _ in range(n)]
times.sort(key=lambda x: x[0])
heap = []
heapq.heappush(heap, times[0][1])

for i in range(1, n):
	if heap[0] > times[i][0]:
		heapq.heappush(heap, times[i][1])
	else:
		heapq.heappop(heap)
		heapq.heappush(heap, times[i][1])

print(len(heap))

백준의 경우 입력시간 초과가 뜰 수 있기 때문에 아래와 같이 입력받는 부분을 적어준다.

import sys
input = sys.stdin.readline

image

스터디 자료

s3 s4 s5 s6 s7

댓글남기기