[백준/JAVA] BOJ 11000- 강의실 배정

NAGANG LEE·2024년 2월 15일

알고

목록 보기
75/118

👀 문제

11000번: 강의실 배정 ✨ 골드 5

수강신청의 마스터 김종혜 선생님에게 새로운 과제가 주어졌다.

김종혜 선생님한테는 Si에 시작해서 Ti에 끝나는 N개의 수업이 주어지는데, 최소의 강의실을 사용해서 모든 수업을 가능하게 해야 한다.

참고로, 수업이 끝난 직후에 다음 수업을 시작할 수 있다. (즉, Ti ≤ Sj 일 경우 i 수업과 j 수업은 같이 들을 수 있다.)

수강신청 대충한 게 찔리면, 선생님을 도와드리자!

예제 입력

3
1 3
2 4
3 5

예제 출력

2

🔑 키포인트

우선순위 큐 그리디 알고리즘 정렬


이게 골5 문제일 리 없음. 넘 힘들었다.

✍️ 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class BOJ11000 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken());
		
		PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
		int[][] times = new int[n][2];

		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			times[i][0] = Integer.parseInt(st.nextToken());
			times[i][1] = Integer.parseInt(st.nextToken());
		}

		// 시작 시간 오름차순으로 정렬하기
		Arrays.sort(times, (s1, s2) -> {
			int minus = s1[0] - s2[0];
			return minus == 0 ? s1[1] - s2[1] : minus;
		});

		pq.add(times[0][1]);
		
		for (int i = 1; i < times.length; i++) {
			int newStart = times[i][0];
			if (newStart >= pq.peek()) {
				pq.poll();
			}
			pq.add(times[i][1]);
		}
		
		
		System.out.println(pq.size());
	}
}
profile
모바일 개발자를 목표로 하고 있어요 💭

0개의 댓글